Ftp服务器文件操作
1 using System;2 using System.IO;
3 using System.Net;
4 using System.Collections.Generic;
5
6 public class FtpUtility
7 {
8 ///
9 /// 获取Ftp服务器上文件列表
10 ///
11 /// Ftp服务器IP
12 /// 用户名
13 /// 口令
14 /// 返回一个文件列表泛型集合
15 public static List GetFiles(string server, string userName, string password)
16 {
17 List fileList = new List();
18
19 try
20 {
21 FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(server));//"ftp://127.0.0.1/book"
22
23 ftpRequest.UseBinary = true;
24 ftpRequest.Credentials = new NetworkCredential(userName, password);
25
26 ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
27
28 WebResponse response = ftpRequest.GetResponse();
29
30 StreamReader sr = new StreamReader(response.GetResponseStream());
31
32 string line = sr.ReadLine();
33
34 while (line != null)
35 {
36 fileList.Add(line);
37 line = sr.ReadLine();
38 }
39
40 sr.Close();
41 response.Close();
42 }
43 catch
44 {
45
46 }
47
48 return fileList;
49 }
50
51 ///
52 /// 下载Ftp服务器上指定文件
53 ///
54 /// 文件路径
55 /// 用户名
56 /// 口令
57 /// 返回一个布尔值类型的操作结果。true为成功,false为失败。
58 public static bool DownloadFile(string fileName, string userName, string password)
59 {
60 try
61 {
62 //创建输出文件流对象
63 FileStream outputStream = new FileStream(fileName, FileMode.Create);
64 //使用指定的Uri对象初始化Ftp客户端
65 FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));//"ftp://127.0.0.1/book/setup.exe"
66 //指定发送到服务器端的命令为文件下载
67 ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
68 //指定文件传输的数据类型是否启用二进制
69 ftpRequest.KeepAlive = true;
70 ftpRequest.UseBinary = true;
71 //指定连接到服务器端的用户凭证
72 ftpRequest.Credentials = new NetworkCredential(userName, password);
73 //返回服务器端的响应
74 FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
75
76 //创建服务器上的响应流
77 Stream ftpStream = response.GetResponseStream();
78
79
80 //获取从服务器上接受数据的长度
81 long cl = response.ContentLength;
82
83 //设置缓冲区大小
84 int bufferSize = 2048;
85
86 //创建缓冲区
87 byte[] buffer = new byte;
88
89 //从响应流中读取的数据的长度
90 int readCount = ftpStream.Read(buffer, 0, bufferSize);
91
92 while (readCount > 0)
93 {
94 outputStream.Write(buffer, 0, readCount);
95
96 readCount = ftpStream.Read(buffer, 0, bufferSize);
97 }
98
99 ftpStream.Close();
100
101 outputStream.Close();
102
103 response.Close();
104 return true;
105 }
106 catch
107 {
108 return false;
109 }
110 }
111
112 ///
113 /// 删除Ftp服务器上指定文件
114 ///
115 /// 文件路径
116 /// 用户名
117 /// 口令
118 /// 返回一个布尔值类型的操作结果。true为成功,false为失败。
119 public static bool DeleteFile(string fileName, string userName, string password)
120 {
121 try
122 {
123 //使用指定的Uri对象初始化Ftp客户端
124 FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));//"ftp://127.0.0.1/book/setup.exe"
125 //指定发送到服务器端的命令为文件下载
126 ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
127 //指定文件传输的数据类型是否启用二进制
128 //reqFtp.UseBinary = true;
129 //指定连接到服务器端的用户凭证
130 ftpRequest.Credentials = new NetworkCredential(userName, password);
131 //返回服务器端的响应
132 FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
133
134 response.Close();
135 return true;
136 }
137 catch
138 {
139 return false;
140 }
141 }
142 }
143
页:
[1]