#region 上传文件 /// /// 上传文件 /// /// 要上传到FTP服务器的文件 /// public static void UpLoadFile(string localFile, string ftpPath, string ftpUser, string ftpPassword) { if (ftpUser == null) { ftpUser = ""; } if (ftpPassword == null) { ftpPassword = ""; } if (!File.Exists(localFile)) { MyLog.ShowMessage("文件:“" + localFile + "” 不存在!"); return; } FtpWebRequest ftpWebRequest = null; FileStream localFileStream = null; Stream requestStream = null; try { ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath)); ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); ftpWebRequest.UseBinary = true; ftpWebRequest.KeepAlive = false; ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpWebRequest.ContentLength = localFile.Length; int buffLength = 4096; byte[] buff = new byte[buffLength]; int contentLen; localFileStream = new FileInfo(localFile).OpenRead(); requestStream = ftpWebRequest.GetRequestStream(); contentLen = localFileStream.Read(buff, 0, buffLength); while (contentLen != 0) { requestStream.Write(buff, 0, contentLen); contentLen = localFileStream.Read(buff, 0, buffLength); } } catch (Exception ex) { MyLog.ShowMessage(ex.Message, "FileUpLoad0001"); } finally { if (requestStream != null) { requestStream.Close(); } if (localFileStream != null) { localFileStream.Close(); } } } #endregion #region 上传文件夹 /// /// 获取目录下的详细信息 /// /// 本机目录 /// private static List> GetDirDetails(string localDir) { List> infos = new List>(); try { infos.Add(Directory.GetFiles(localDir).ToList()); //获取当前目录的文件 infos.Add(Directory.GetDirectories(localDir).ToList()); //获取当前目录的目录 for (int i = 0; i < infos[0].Count; i++) { int index = infos[0][i].LastIndexOf(@"\"); infos[0][i] = infos[0][i].Substring(index + 1); } for (int i = 0; i < infos[1].Count; i++) { int index = infos[1][i].LastIndexOf(@"\"); infos[1][i] = infos[1][i].Substring(index + 1); } } catch (Exception ex) { ex.ToString(); } return infos; } /// /// 上传整个目录 /// /// 要上传的目录的上一级目录 /// FTP路径 /// 要上传的目录名 /// FTP用户名(匿名为空) /// FTP登录密码(匿名为空) public static void UploadDirectory(string localDir, string ftpPath, string dirName, string ftpUser, string ftpPassword) { if (ftpUser == null) { ftpUser = ""; } if (ftpPassword == null) { ftpPassword = ""; } string dir = localDir + dirName + @"\"; //获取当前目录(父目录在目录名) if (!Directory.Exists(dir)) { MyLog.ShowMessage("目录:“" + dir + "” 不存在!"); return; } if (!CheckDirectoryExist(ftpPath,dirName)) { MakeDir(ftpPath, dirName); } List> infos = GetDirDetails(dir); //获取当前目录下的所有文件和文件夹 //先上传文件 MyLog.ShowMessage(dir + "下的文件数:" + infos[0].Count.ToString()); for (int i = 0; i < infos[0].Count; i++) { Console.WriteLine(infos[0][i]); UpLoadFile(dir + infos[0][i], ftpPath + dirName + @"/" + infos[0][i], ftpUser, ftpPassword); } //再处理文件夹 MyLog.ShowMessage(dir + "下的目录数:" + infos[1].Count.ToString()); for (int i = 0; i < infos[1].Count; i++) { UploadDirectory(dir, ftpPath + dirName + @"/", infos[1][i], ftpUser, ftpPassword); } } /// /// 新建目录 /// /// /// public static void MakeDir(string ftpPath, string dirName) { try { //实例化FTP连接 FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + dirName)); //指定FTP操作类型为创建目录 request.Method = WebRequestMethods.Ftp.MakeDirectory; //获取FTP服务器的响应 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); } catch (Exception ex) { MyLog.ShowMessage(ex.Message, "MakeDir"); } } /// /// 检查目录是否存在 /// /// 要检查的目录的上一级目录 /// 要检查的目录名 /// 存在返回true,否则false public static bool CheckDirectoryExist(string ftpPath,string dirName) { bool result = false; try { //实例化FTP连接 FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath)); //指定FTP操作类型为创建目录 request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //获取FTP服务器的响应 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); StringBuilder str = new StringBuilder(); string line = sr.ReadLine(); while (line != null) { str.Append(line); str.Append("|"); line = sr.ReadLine(); } string[] datas = str.ToString().Split('|'); for (int i = 0; i < datas.Length; i++) { if (datas[i].Contains(" ")) { int index = datas[i].IndexOf(""); string name = datas[i].Substring(index + 5).Trim(); if (name==dirName) { result = true; break; } } } sr.Close(); sr.Dispose(); response.Close(); } catch (Exception ex) { MyLog.ShowMessage(ex.Message, "MakeDir"); } return result; } 调用: //FTP地址 string ftpPath = @"Ftp://192.168.1.68:21/"; //本机要上传的目录的父目录 string localPath = @"D:\"; //要上传的目录名 string fileName = @"haha"; FTPClient.UploadDirectory(localPath, ftpPath, fileName, "", ""); |