读取文件列表:
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://rockniu.qsh.eu/www/XmlData");
Request.Method = WebRequestMethods.Ftp.ListDirectory; //列表
Request.Credentials = new NetworkCredential("loginid", "password");
FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
Stream ResponseStream = Response.GetResponseStream();
StreamReader Reader = new StreamReader(ResponseStream);
//this.listBox1.Items.Add(Response.BannerMessage);
//this.listBox1.Items.Add(Response.WelcomeMessage);
while (!Reader.EndOfStream)//读取列表
{
//this.listBox2.Items.Add(Reader.ReadLine());
}
Reader.Close();
Response.Close();
ResponseStream.Close();
上传文件:
void UploadFile(string name, string path)
{
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://rockniu.qsh.eu/www/XmlData/" + name);
Request.Method = WebRequestMethods.Ftp.ListDirectory; //列表
Request.Credentials = new NetworkCredential("login", "password");
Request.Method = WebRequestMethods.Ftp.UploadFile;
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(path);
Stream requestStream = null;
requestStream = Request.GetRequestStream();
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
} while (readBytes != 0);
stream.Close();
requestStream.Close();
}