配置参数
public string FtpURL = " "; //地址
public string FtpUser = " "; //ftp账号
public string FtpPassWord = " "; //ftp密码
上传文件
public void UpLoad(string 文件路径)
{
FileInfo info = new FileInfo(文件路径);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(上传文件目标路径"));
reqFTP.Credentials = new NetworkCredential(FtpUser,FtpPassWord);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = info.Length;
int bufflength = 2048;
byte[] buff = new byte[bufflength];
int contentlen;
FileStream fs = info.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentlen = fs.Read(buff, 0, bufflength);
while(contentlen != 0 )
{
strm.Write(buff, 0, contentlen);
contentlen = fs.Read(buff, 0, bufflength);
}
UpLoadBySQL(NowKey);
MessageBox.Show("FTP上傳成功!","成功");
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
下载
public void DownLoad(string FTP文件路径)
{
Uri uri = new Uri(FTP文件路径);
string tmpName = Guid.NewGuid().ToString();
string localfile = 下载目标路径;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FtpUser, FtpPassWord);
FtpWebResponse res = (FtpWebResponse)reqFtp.GetResponse();
Stream ftpstream = res.GetResponseStream();
long cl = res.ContentLength;
int buffsize = 2048;
int readcount;
byte[] buffer = new byte[buffsize];
readcount = ftpstream.Read(buffer, 0, buffsize);
FileStream output = new FileStream(localfile, FileMode.Create);
while(readcount >0)
{
output.Write(buffer, 0, readcount);
readcount = ftpstream.Read(buffer, 0, buffsize);
}
ftpstream.Close();
output.Close();
res.Close();
}
Comments | NOTHING