110,535
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
int qq = DownloadFtp("e:\\", "1234.txt", "192.168.1.18", "dsfrpt", "dsfrpt0420");
string errormsg = "";
//bool flag = IsCanConnectFtp("dsfrpt", "dsfrpt0420", out errormsg);
if (qq==0)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("失败");
}
}
public static int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + fileName);
MessageBox.Show(fileInf.Name);
FtpWebRequest reqFTP;
try
{
//filePath = <<The full path where the file is to be created.>>,
//fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
//FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
string[] aa = filePath.Split('\\');
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/upload/" + fileInf.Name));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
//reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("dsfrpt", "dsfrpt0420");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = null;
MessageBox.Show(readCount.ToString());
if (readCount > 0)
{
outputStream = new FileStream("d:\\测试.txt", FileMode.Create);
}
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return 0;
}
catch (Exception ex)
{
//Logging.WriteError(ex.Message + ex.StackTrace);
return -2;
}
}