private static string ftpServerIP = "ftp://192.168.1.1";//服务器ip
private static string ftpUserID = "name";//用户名
private static string ftpPassword = "123456";//密码
/// <summary>
/// 上传文件
/// </summary>
/// <param name="localFile">要上传到FTP服务器的本地文件</param>
/// <param name="ftpPath">FTP地址</param>
public static bool UpLoadFile(string localFile)
{
if (!File.Exists(localFile))
{
MessageBox.Show("文件:“" + localFile + "” 不存在!");
return false;
}
FileInfo fileInf = new FileInfo(localFile);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpServerIP + "/" + fileInf.Name);// 根据uri创建FtpWebRequest对象
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
//reqFTP.Credentials = new NetworkCredential(SetingInfo.GetAreaCode()+"_"+SetingInfo.GetOrgCode(), SetingInfo.GetServerPassword());// ftp用户名和密码
reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
reqFTP.UseBinary = true;// 指定数据传输类型
reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
int buffLength = 2048;// 缓冲大小设置为2kb
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
while (contentLen != 0)// 流内容没有结束
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
//MessageBox.Show("文件【" + ftpPath + "/" + fileInf.Name + "】上传成功!<br/>");
MessageBox.Show("文件【" + fileInf.Name + "】上传成功!");
return true;
}
catch (Exception ex)
{
MessageBox.Show("上传文件【" + fileInf.Name + "】时,发生错误:" + ex.Message);
return false;
}
}
//从ftp服务器上下载文件的功能
public void Download(string savepath,string fileName)
{
FtpWebRequest reqFTP;
try
{
//string filePath = Application.StartupPath;
string filePath = savepath;
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UsePassive = false;
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);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
MessageBox.Show("文件【" + fileName + "】下载成功!");
}
catch (Exception ex)
{
throw ex;
}
}
///
///删除远程服务器文件
///
/// 文件名
///
public bool DeleteFileName(string fileName)
{
if (!File.Exists(fileName))
{
MessageBox.Show(“文件:“” + fileName + “” 不存在!”);
return false;
}
try
{
FileInfo fileInf = new FileInfo(fileName);
string uri = ftpServerIP + "/" + fileInf.Name;
//string uri = "ftp://116.52.249.232" + "/" + fileInf.Name;
Connect(uri);//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
MessageBox.Show(fileName + "文件删除成功!");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "删除错误");
return false;
}
}