FTP客户端开发

杜思波2008 

最近公司有个项目需要用到FTP相关功能,老总把这个光荣的使命交给了我来完成,当然我肯定不能也不敢怠慢,说时迟那时快,接到指令我就和SHELL一样开始工作了。
    首先我们讲一下要实现FTP要用到什么:
第一命名空间:using System.Net,使用改命名空间可以引用FtpWebRequest相关功能函数。
下面我公布一下我的代码文件,提供给大家参考:

  1.   #region 上传文件
  2.   /// <summary>
  3.   /// 上传文件  
  4.   /// </summary>
  5.   /// <param name="sender"></param>
  6.   /// <param name="e"></param>
  7.   private void Update_Click(object sender, EventArgs e)
  8.   {
  9.    
  10.    Update.Multiselect = true;
  11.    Update.Title = "XXFTP专用";
  12.    Update.Filter = "所有文件|*.*";
  13.    if (Update.ShowDialog() == DialogResult.OK)
  14.    {
  15.     if (Update.FileNames.Length == 0)
  16.     {
  17.      MessageBox.Show("请选择文件");
  18.     }
  19.     else
  20.     {
  21.      Thread SendFile = new Thread(UpdateFile);//注意这里,使用线程,如果不是用线程,再实现功能时候,界面会很慢,主要是主线程被占用~~~~~~~
  22.      SendFile.Start();
  23.     }
  24.    }
  25.   }
  26.   /// <summary>
  27.   /// 上传文件
  28.   /// </summary>
  29.   /// <param name="FileName"></param>
  30.   private void UpdateFile()
  31.   {
  32.    delete.Enabled= Clears.Enabled=DownLoadFiles.Enabled=PeoZhi.Enabled= Updates.Enabled = false;
  33.    for (int file = 0; file < Update.FileNames.Length; file++)
  34.    {
  35.     SoftInfo.Items.Add("选中文件:" + Update.FileNames[file].ToString());  
  36.     string FileName = Update.FileNames[file].ToString();
  37.     SoftInfo.Items.Add("正在上传中..................").ForeColor = Color.Green;
  38.     try
  39.     {
  40.      Ftp(WebRequestMethods.Ftp.UploadFile,FileName);
  41.     }
  42.     catch (Exception ex)
  43.     {
  44.      SoftInfo.Items.Add("上传失败 :(").ForeColor = Color.Red;
  45.     }
  46.    }
  47.    delete.Enabled = Clears.Enabled = DownLoadFiles.Enabled = PeoZhi.Enabled = Updates.Enabled = true;
  48.    LoadServerFile(WebRequestMethods.Ftp.ListDirectory);
  49.   }
  50.   /// <summary>
  51.   /// Ftp公共调用函数,用来连接Ftp服务器,并且对相应文件操作
  52.   /// </summary>
  53.   /// <param name="Type">操作类型</param>
  54.   /// <param name="FileName">相关文件的文件名</param>
  55.   private void Ftp(string Type,string FileName)
  56.   {
  57.    FileInfo Files = new FileInfo(FileName);
  58.    string Url = "ftp://" + ServerIP + "/" + FileName;
  59.    FtpWebRequest RequestFtp;
  60.    RequestFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ServerIP + "/" + Files.Name));
  61.    RequestFtp.Credentials = new NetworkCredential(UserID, Password);
  62.    RequestFtp.KeepAlive = false;
  63.    RequestFtp.Method = Type;
  64.    RequestFtp.UseBinary = true;
  65.    RequestFtp.ContentLength = Files.Length;
  66.    int buffsize = 2048;
  67.    byte[] buff = new byte[buffsize];
  68.    int contentLen;
  69.    FileStream fs = Files.OpenRead();
  70.    Stream stm = RequestFtp.GetRequestStream();
  71.    contentLen = fs.Read(buff, 0, buffsize);
  72.    while (contentLen != 0)
  73.    {
  74.     stm.Write(buff, 0, contentLen);
  75.     contentLen = fs.Read(buff, 0, contentLen);//长度
  76.    }
  77.    SoftInfo.Items.Add("上传成功 :)").ForeColor = Color.Green;
  78.    
  79.    //关闭相应流
  80.    stm.Close();
  81.    fs.Close();
  82.   }
  83.   #endregion
  84. #region 下载服务器文件
  85.   private void DownLoad_Click(object sender, EventArgs e)
  86.   {
  87.    if (Download.ShowDialog() == DialogResult.OK)
  88.    {
  89.     Thread Down = new Thread(new ThreadStart(DownLoad));
  90.     Down.Start();
  91.    }
  92.   }
  93.   private void DownLoad()
  94.   {
  95.    for (int files = 0; files < FileList.Items.Count; files++)
  96.     DownloadFile(Download.SelectedPath, FileList.Items[files].Text);
  97.    SoftInfo.Items.Add("文件下载完毕,保存在:" + Download.SelectedPath).ForeColor=Color.Green;
  98.   }
  99.   /// <summary>
  100.   /// 文件下载
  101.   /// </summary>
  102.   /// <param name="filePath">文件路径</param>
  103.   /// <param name="fileName">文件名</param>
  104.   private void DownloadFile(string filePath, string fileName)
  105.   {
  106.    FtpWebRequest reqFTP;
  107.    try
  108.    {
  109.     FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create);
  110.     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ServerIP + "/" + fileName));
  111.     reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  112.     reqFTP.UseBinary = true;
  113.     reqFTP.Credentials = new NetworkCredential(UserID, Password);
  114.     FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  115.     Stream ftpStream = response.GetResponseStream();
  116.     long cl = response.ContentLength;
  117.     int bufferSize = 2048;
  118.     int readCount;
  119.     byte[] buffer = new byte[bufferSize];
  120.     readCount = ftpStream.Read(buffer, 0, bufferSize);
  121.     while (readCount > 0)
  122.     {
  123.      outputStream.Write(buffer, 0, readCount);
  124.      readCount = ftpStream.Read(buffer, 0, bufferSize);
  125.     }
  126.     ftpStream.Close();
  127.     outputStream.Close();
  128.     response.Close();
  129.    }
  130.    catch (Exception ex)
  131.    {
  132.     MessageBox.Show(ex.Message);
  133.    }
  134.   }
  135.   #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值