欢迎光临
我们一直在努力

C#FTP上传文件至服务器代码

 

 /// <summary> /// 下载文件 /// </summary> /// <param name="localDir">下载至本地路径</param> /// <param name="ftpDir">ftp目标文件路径</param> /// <param name="FtpFile">从ftp要下载的文件名</param> /// <param name="hostname">ftp地址即IP</param> /// <param name="username">ftp用户名</param> /// <param name="password">ftp密码</param> public static void DownloadFile(string localDir, string FtpDir, string FtpFile, string hostname, string username, string password) { string URI = "FTP://" + hostname + "/" + FtpDir + "/" + FtpFile; string tmpname = Guid.NewGuid().ToString(); string localfile = localDir + @"\" + tmpname; System.Net.FtpWebRequest ftp = GetRequest(URI, username, password); ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile; ftp.UseBinary = true; ftp.UsePassive = false; using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { //loop to read & write to file using (FileStream fs = new FileStream(localfile, FileMode.CreateNew)) { try { byte[] buffer = new byte[2048]; int read = 0; do { read = responseStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, read); } while (!(read == 0)); responseStream.Close(); fs.Flush(); fs.Close(); } catch (Exception) { //catch error and delete file only partially downloaded  fs.Close(); //delete target file as it's incomplete  File.Delete(localfile); throw; } } responseStream.Close(); } response.Close(); } try { File.Delete(localDir + @"\" + FtpFile); File.Move(localfile, localDir + @"\" + FtpFile); ftp = null; ftp = GetRequest(URI, username, password); ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; ftp.GetResponse(); } catch (Exception ex) { File.Delete(localfile); throw ex; } // 记录日志 "从" + URI.ToString() + "下载到" + localDir + @"\" + FtpFile + "成功." ); ftp = null; } /// <summary> /// 搜索远程文件 /// </summary> /// <param name="targetDir"></param> /// <param name="hostname"></param> /// <param name="username"></param> /// <param name="password"></param> /// <param name="SearchPattern"></param> /// <returns></returns> public static List<string> ListDirectory(string targetDir, string hostname, string username, string password, string SearchPattern) { List<string> result = new List<string>(); try { string URI = "FTP://" + hostname + "/" + targetDir + "/" + SearchPattern; System.Net.FtpWebRequest ftp = GetRequest(URI, username, password); ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectory; ftp.UsePassive = true; ftp.UseBinary = true; string str = GetStringResponse(ftp); str = str.Replace("\r\n", "\r").TrimEnd('\r'); str = str.Replace("\n", "\r"); if (str != string.Empty) result.AddRange(str.Split('\r')); return result; } catch { } return null; } private static string GetStringResponse(FtpWebRequest ftp) { //Get the result, streaming to a string string result = ""; using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse()) { long size = response.ContentLength; using (Stream datastream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(datastream, System.Text.Encoding.Default)) { result = sr.ReadToEnd(); sr.Close(); } datastream.Close(); } response.Close(); } return result; }

 

 

 

 

 

 

 

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《C#FTP上传文件至服务器代码》
文章链接:https://www.456zj.com/534.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    I like it when people come together and share views.
    Great site, keep it up!

    dp5682年前 (2022-09-13)Reply