下载文件
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="filename"></param>
- private static void DownLoadFile(string filename)
- {
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename);
- req.Method = WebRequestMethods.Ftp.DownloadFile;
- req.UseBinary = true;
- req.UsePassive = true;
- req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
- try
- {
- using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
- {
- string localfile = Path.Combine(LocalPath, filename);
- FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
- int buffer = 1024; //1K缓冲
- byte[] b = new byte[buffer];
- int i = 0;
- Stream stream = res.GetResponseStream();
- while ((i = stream.Read(b, 0, buffer)) > 0)
- {
- fs.Write(b, 0, i);
- }
- }
- Console.WriteLine(filename + " download!");
- Log(filename + "下载成功");
-
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- Log(ex.ToString());
- }
- finally
- {
-
- }
- }
/// <summary> /// 下载文件 /// </summary> /// <param name="filename"></param> private static void DownLoadFile(string filename) { FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename); req.Method = WebRequestMethods.Ftp.DownloadFile; req.UseBinary = true; req.UsePassive = true; req.Credentials = new NetworkCredential(FtpUid, FtpPwd); try { using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { string localfile = Path.Combine(LocalPath, filename); FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write); int buffer = 1024; //1K缓冲 byte[] b = new byte[buffer]; int i = 0; Stream stream = res.GetResponseStream(); while ((i = stream.Read(b, 0, buffer)) > 0) { fs.Write(b, 0, i); } } Console.WriteLine(filename + " download!"); Log(filename + "下载成功"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Log(ex.ToString()); } finally { } }
获取文件列表
- /// <summary>
- /// 获取FTP文件列表
- /// </summary>
- /// <returns></returns>
- private static List<String> GetFileList()
- {
- List<string> list = new List<string>();
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath));
- req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
- req.Method = WebRequestMethods.Ftp.ListDirectory;
- req.UseBinary = true;
- req.UsePassive = true;
- try
- {
- using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
- {
- using (StreamReader sr = new StreamReader(res.GetResponseStream()))
- {
- string s;
- while ((s = sr.ReadLine()) != null)
- {
-
- list.Add(s);
- }
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- Log("下载文件列表失败:");
- Log(ex.ToString());
- }
- return list;
-
- }
/// <summary> /// 获取FTP文件列表 /// </summary> /// <returns></returns> private static List<String> GetFileList() { List<string> list = new List<string>(); FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath)); req.Credentials = new NetworkCredential(FtpUid, FtpPwd); req.Method = WebRequestMethods.Ftp.ListDirectory; req.UseBinary = true; req.UsePassive = true; try { using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { using (StreamReader sr = new StreamReader(res.GetResponseStream())) { string s; while ((s = sr.ReadLine()) != null) { list.Add(s); } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Log("下载文件列表失败:"); Log(ex.ToString()); } return list; }
上传文件
- private static void UploadFile(string localFile)
- {
- FileInfo fi = new FileInfo(localFile);
- FileStream fs = fi.OpenRead();
- long length = fs.Length;
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
- req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
- req.Method = WebRequestMethods.Ftp.UploadFile;
- req.UseBinary = true;
- req.ContentLength = length;
- req.Timeout = 10 * 1000;
- try
- {
- Stream stream = req.GetRequestStream();
-
- int BufferLength = 2048; //2K
- byte[] b = new byte[BufferLength];
- int i;
- while ((i = fs.Read(b, 0, BufferLength)) > 0)
- {
- stream.Write(b, 0, i);
- }
- stream.Close();
- stream.Dispose();
-
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
-
- }
|
请发表评论