//上传文件 public static Boolean FtpUpload(string ftpPath, string localFile, FtpServer svr) { //检查目录是否存在,不存在创建 FtpCheckDirectoryExist(ftpPath, svr); FileInfo fi = new FileInfo(localFile); FileStream fs = fi.OpenRead(); long length = fs.Length; FtpWebRequest req = (FtpWebRequest)WebRequest.Create(svr.FtpServerIp + ftpPath + fi.Name); req.Credentials = new NetworkCredential(svr.FtpUserId, svr.FtpPassword); req.Method = WebRequestMethods.Ftp.UploadFile; req.ContentLength = length; req.Timeout = * ; try { Stream stream = req.GetRequestStream(); ; //2K byte[] b = new byte[BufferLength]; int i; , BufferLength)) > ) { stream.Write(b, , i); } stream.Close(); stream.Dispose(); } catch (Exception) { return false; } finally { fs.Close(); req.Abort(); } req.Abort(); return true; } //判断文件的目录是否存,不存则创建 public static void FtpCheckDirectoryExist(string destFilePath, FtpServer svr) { string fullDir = FtpParseDirectory(destFilePath); string[] dirs = fullDir.Split('/'); string curDir = "/"; ; i < dirs.Length; i++) { string dir = dirs[i]; //如果是以/开始的路径,第一个为空 if (!string.IsNullOrEmpty(dir)) { try { curDir += dir + "/"; FtpMakeDir(curDir, svr); } catch (Exception) { // ignored } } } } public static string FtpParseDirectory(string destFilePath) { , destFilePath.LastIndexOf("/", StringComparison.Ordinal)); } //创建目录 public static Boolean FtpMakeDir(string localFile, FtpServer svr) { FtpWebRequest req = (FtpWebRequest)WebRequest.Create(svr.FtpServerIp + localFile); req.Credentials = new NetworkCredential(svr.FtpUserId, svr.FtpPassword); req.Method = WebRequestMethods.Ftp.MakeDirectory; try { FtpWebResponse response = (FtpWebResponse)req.GetResponse(); response.Close(); } catch (Exception) { req.Abort(); return false; } req.Abort(); return true; } public class FtpServer { public string FtpServerIp { get; set; } public string FtpPath { get; set; } public string FtpUserId { get; set; } public string FtpPassword { get; set; } }