当前位置:  编程技术>.net/c#/asp.net

c#操作ftp类分享

    来源: 互联网  发布时间:2014-10-28

    本文导语:  代码如下:class ftp{    private string host = null;    private string user = null;    private string pass = null;    private FtpWebRequest ftpRequest = null;    private FtpWebResponse ftpResponse = null;    private Stream ftpStream = null;    private int bufferSize = 2048;...

代码如下:

class ftp
{
    private string host = null;
    private string user = null;
    private string pass = null;
    private FtpWebRequest ftpRequest = null;
    private FtpWebResponse ftpResponse = null;
    private Stream ftpStream = null;
    private int bufferSize = 2048;

    public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

    public void download(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

    public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

    public void delete(string deleteFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

    public void rename(string currentFileNameAndPath, string newFileName)
    {
        try
        {
            ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.Rename;
            ftpRequest.RenameTo = newFileName;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

    public void createDirectory(string newDirectory)
    {
        try
        {
            ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

    public string getFileCreatedDateTime(string fileName)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            StreamReader ftpReader = new StreamReader(ftpStream);
            string fileInfo = null;
            try { fileInfo = ftpReader.ReadToEnd(); }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
            return fileInfo;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return "";
    }

    public string getFileSize(string fileName)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            StreamReader ftpReader = new StreamReader(ftpStream);
            string fileInfo = null;
            try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
            return fileInfo;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return "";
    }

    public string[] directoryListSimple(string directory)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            StreamReader ftpReader = new StreamReader(ftpStream);
            string directoryRaw = null;
            try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
            try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return new string[] { "" };
    }

    public string[] directoryListDetailed(string directory)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            StreamReader ftpReader = new StreamReader(ftpStream);
            string directoryRaw = null;
            try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
            try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return new string[] { "" };
    }
}

代码如下:

ftp ftpClient = new ftp(@"ftp://10.10.10.10/", "user", "password");
ftpClient.upload("etc/test.txt", @"C:UsersmetastructDesktoptest.txt");
ftpClient.download("etc/test.txt", @"C:UsersmetastructDesktoptest.txt");
ftpClient.delete("etc/test.txt");
ftpClient.rename("etc/test.txt", "test2.txt");
ftpClient.createDirectory("etc/test");
string fileDateTime = ftpClient.getFileCreatedDateTime("etc/test.txt");
Console.WriteLine(fileDateTime);
string fileSize = ftpClient.getFileSize("etc/test.txt");
Console.WriteLine(fileSize);
string[] simpleDirectoryListing = ftpClient.directoryListDetailed("/etc");
for (int i = 0; i < simpleDirectoryListing.Count(); i++) { Console.WriteLine(simpleDirectoryListing[i]);
string[] detailDirectoryListing = ftpClient.directoryListDetailed("/etc");
for (int i = 0; i < detailDirectoryListing.Count(); i++) { Console.WriteLine(detailDirectoryListing[i]); }
ftpClient = null;

    
 
 

您可能感兴趣的文章:

  • c#对象中两种copy操作:深拷贝(Deep Copy)与浅拷贝(Shallow Copy)
  • c#的时间日期操作示例分享(c#获取当前日期)
  • .NET下 c#通过COM组件操作并导出Excel实例代码
  • C#操作txt文件,进行清空添加操作的小例子
  • C#实现装箱与拆箱操作简单实例
  • 浅谈C#互操作的内存溢出问题
  • C# 中的??操作符浅谈
  • c#剪切板操作的简单实例
  • c# 调用Surfer软件,添加引用的具体操作方法
  • c#异步task示例分享(异步操作)
  • c#下注册表操作的一个小细节
  • C#操作CLOB大对象的代码一例
  • c#判断操作系统位数实例代码
  • 一些关于c#与Sql的时间的操作
  • c#判断操作系统位数的示例分享
  • C#中的位操作小结
  • C# 操作符之三元操作符浅析
  • C# Dictionary操作范例(入门新手参考)
  • C#的WebBrowser操作frame实例解析
  • C# Winform 操作 INI 配置文件的实现代码
  • C#程序最小化到托盘图标操作步骤与实现代码
  • python赋值操作方法分享
  • aspx中的mysql操作类sqldatasource使用示例分享
  • jquery操作checkbox示例分享
  • svn服务器启动和svn服务器重启、停止等操作脚本分享
  • java读取文件内容的三种方法代码片断分享(java文件操作)
  • python文件读写并使用mysql批量插入示例分享(python操作mysql)
  • jquery操作cookie插件分享
  • jQuery 操作下拉列表框的实例分享
  • java实现操作系统的短进程作业调度示例分享
  • jquery操作HTML5 的data-*的用法实例分享
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • C++ Stacks(堆栈) 成员 操作:比较和分配堆栈
  • 谁有操作系统PV操作的例子???谁有操作系统PV操作的例子???谢谢!!
  • C++ Strings(字符串) 成员 Operators:操作符,用于字符串比较和赋值
  • 已安装了Windows操作系统,还想安装Linux。却还想在开机选择操作系统时由Windows引导,请问如何操作。在线等待
  • C++ I/O 成员 flags():操作flags
  • 请问LINUX操作系统是怎样对外围设备进行操作的
  • C++ I/O 成员 width():操作域宽度
  • 什么样的操作最耗费服务器的IO操作?
  • Xcode介绍及创建工程和工程依赖操作步骤
  • 无操作系统下对U盘的操作
  • MyEclipse如何查看和设置文件编码格式相关操作
  • 请问命令行操作下怎么改Linux操作系统的日期和时间?
  • Html checkbox标签如何设置默认选中以及用js操作checkbox代码示例
  • 正在学操作系统原理,做操作系统方面的实验用那种工具较好?
  • c/c++ 操作符优先级参考
  • 请问16位操作系统和32位操作系统的区别?
  • javascript操作html复选框checkbox:如何判断复选框是否被选中
  • 基于linux操作系统之上操作LCD问题,急???
  • Plesk 中操作和设置 Docker 容器
  • 哪位大侠知道Linux里的有名管道传送数据快不快?有名管道操作的过程是否有读写硬盘的操作?
  • java操作excel2007文档介绍及代码例子
  • 如何在一个用户对application操作时防止别的用户对它操作?(好难啊)


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3