日期:2014-05-17 浏览次数:20537 次
private static void UploadFile(string localFile)
{
FileInfo fi = new FileInfo(localFile);
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048;
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}