0


C# ftp帮助类 项目实战优化版

  1. 上位机开发中有时要与客户的文件服务器进行数据交互。如Mapping文件下载。结果文件上传等。我在项目中就常用到。现在把项目实战代码进行分享一下。
  2. 功能列表:连接服务器,下载文件,上传文件,删除服务器文件,获取当前目录下明细(包含文件和文件夹) ,获取FTP文件列表(包括文件夹),获取当前目录下文件列表(不包括文件夹) ,创建文件夹,获取指定文件大小 ,更改文件名,移动文件 ,删除ftpURI目录下指定的文件夹

调用

  1. 1.初始化连接
  2. CommonData.FtpServer_MappingFile = new FTPHelper(ftpIPAddress, ftpUser, ftpPw);
  3. 2.下载
  4. //url 服务器下载地址
  5. //savefilePath 保存文件目录
  6. //fileName 文件名,只是文件名
  7. CommonData.FtpServer_MappingFile.DownloadByUrl(url, savefilePath, fileName, true);
  8. 3.上传
  9. //currentDirectory 本地文件完整路径包括文件名
  10. //ftpUploadFileName FTP服务器完整上传路径包括文件名
  11. CommonData.FtpServer_MappingFile.Upload(currentDirectory, ftpUploadFileName);

代码

  1. /// <summary>
  2. /// ftp帮助类
  3. /// </summary>
  4. public class FTPHelper
  5. {
  6. #region 字段
  7. string ftpURI;
  8. string ftpUserID;
  9. string ftpServerIP;
  10. string ftpPassword;
  11. string ftpRemotePath;
  12. /// <summary>
  13. /// 下载URL
  14. /// </summary>
  15. public string DownloadUrl { get; set; }
  16. #endregion
  17. public string GetFtpURI()
  18. {
  19. return ftpURI;
  20. }
  21. //public FTPHelper() { }
  22. /// <summary>
  23. /// 连接FTP服务器
  24. /// </summary>
  25. /// <param name="FtpServerIP">FTP连接地址</param>
  26. /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
  27. /// <param name="FtpUserID">用户名</param>
  28. /// <param name="FtpPassword">密码</param>
  29. public FTPHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword, bool isFile = false)
  30. {
  31. ftpServerIP = FtpServerIP;
  32. ftpRemotePath = FtpRemotePath;
  33. ftpUserID = FtpUserID;
  34. ftpPassword = FtpPassword;
  35. //ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
  36. ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
  37. if (isFile)
  38. {
  39. ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath.Replace("\\", "/");
  40. }
  41. }
  42. /// <summary>
  43. /// 连接FTP服务器
  44. /// </summary>
  45. /// <param name="FtpServerIP">FTP连接地址</param>
  46. /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
  47. /// <param name="FtpUserID">用户名</param>
  48. /// <param name="FtpPassword">密码</param>
  49. public FTPHelper(string FtpServerIP, string FtpUserID, string FtpPassword)
  50. {
  51. ftpServerIP = FtpServerIP;
  52. //ftpRemotePath = FtpRemotePath;
  53. ftpUserID = FtpUserID;
  54. ftpPassword = FtpPassword;
  55. //ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
  56. ftpURI = "ftp://" + ftpServerIP;//+ "/" + ftpRemotePath + "/";
  57. //if (isFile)
  58. //{
  59. // ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath.Replace("\\", "/");
  60. //}
  61. }
  62. /// <summary>
  63. /// 上传 filename是本地图片的地址
  64. /// </summary>
  65. public void Upload(string filename)
  66. {
  67. FileInfo fileInf = new FileInfo(filename);
  68. FtpWebRequest reqFTP;
  69. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name));
  70. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  71. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  72. reqFTP.KeepAlive = false;
  73. reqFTP.UseBinary = true;
  74. reqFTP.ContentLength = fileInf.Length;
  75. int buffLength = 2048;
  76. byte[] buff = new byte[buffLength];
  77. int contentLen;
  78. FileStream fs = fileInf.OpenRead();
  79. try
  80. {
  81. Stream strm = reqFTP.GetRequestStream();
  82. contentLen = fs.Read(buff, 0, buffLength);
  83. while (contentLen != 0)
  84. {
  85. strm.Write(buff, 0, contentLen);
  86. contentLen = fs.Read(buff, 0, buffLength);
  87. }
  88. strm.Close();
  89. fs.Close();
  90. }
  91. catch (Exception ex)
  92. {
  93. throw new Exception(ex.Message);
  94. }
  95. }
  96. /// <summary>
  97. /// 上传
  98. /// </summary>
  99. /// <param name="fileName">是本地文件全路径</param>
  100. /// <param name="ftpUrl"></param>
  101. /// <param name="lmDir"></param>
  102. /// <returns></returns>
  103. public CommonResultModel<string> Upload(string fileName, string ftpUrl)
  104. {
  105. var resultModel = new CommonResultModel<string>();
  106. FileInfo fileInf = null;
  107. FtpWebRequest reqFTP;
  108. Stream strm = null;
  109. FileStream fs = null;
  110. var uploadUrl = string.Empty;
  111. try
  112. {
  113. //ip/指定目录/lotId/pid.csv
  114. uploadUrl = ftpURI + ftpUrl;
  115. resultModel.Data = uploadUrl;
  116. //var ftpDir = uploadUrl.Replace(lmDir, string.Empty);
  117. var ftpDirList = ftpUrl.Split('/').ToList();
  118. ftpDirList = ftpDirList.FindAll(t => t.Length > 0).FindAll(t => !t.Contains("."));
  119. var newDir = ftpURI;
  120. //判断文件夹是否存在
  121. //resultModel.Msg = "Upload 判断文件夹是否存在 ";
  122. //CommonDefine.SaveWorkLogs(resultModel.Msg);
  123. foreach (var item in ftpDirList)
  124. {
  125. //newDir += "/" + item;
  126. var isExists = FolderExists(newDir, item);
  127. newDir += "/" + item;
  128. //resultModel.Msg += "newDir:" + newDir + ",isExists:" + isExists + ",";
  129. //CommonDefine.SaveWorkLogs(resultModel.Msg);
  130. if (!isExists)
  131. {
  132. var makeDirResultModel = MakeDir(newDir);
  133. //CommonDefine.SaveWorkLogs(resultModel.Msg
  134. // + ", MakeDir: IsSucceed=" + makeDirResultModel.IsSucceed + ",msg:" + makeDirResultModel.Msg);
  135. //if (!makeDirResultModel.IsSucceed)
  136. //{
  137. // throw new Exception(makeDirResultModel.Msg);
  138. //}
  139. }
  140. }
  141. fileInf = new FileInfo(fileName);
  142. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl));
  143. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  144. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  145. reqFTP.KeepAlive = false;
  146. reqFTP.UseBinary = true;
  147. reqFTP.ContentLength = fileInf.Length;
  148. int buffLength = 2048;
  149. byte[] buff = new byte[buffLength];
  150. int contentLen;
  151. fs = fileInf.OpenRead();
  152. strm = reqFTP.GetRequestStream();
  153. contentLen = fs.Read(buff, 0, buffLength);
  154. while (contentLen != 0)
  155. {
  156. strm.Write(buff, 0, contentLen);
  157. contentLen = fs.Read(buff, 0, buffLength);
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. //throw new Exception("uploadUrl:" + uploadUrl + ",Message:" + ex.Message);
  163. resultModel.Msg = "异常:" + ex.Message;
  164. }
  165. finally
  166. {
  167. if (strm != null)
  168. {
  169. strm.Close();
  170. }
  171. if (fs != null)
  172. {
  173. fs.Close();
  174. }
  175. }
  176. return resultModel;
  177. }
  178. /// <summary>
  179. /// 下载 filePath是下载到本机的地址fileName是需要下载的文件的名字
  180. /// </summary>
  181. public void Download(string filePath, string fileName, bool isFile = false)
  182. {
  183. try
  184. {
  185. FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
  186. FtpWebRequest reqFTP;
  187. if (isFile)
  188. {
  189. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
  190. }
  191. else
  192. {
  193. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
  194. }
  195. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  196. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  197. reqFTP.UseBinary = true;
  198. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  199. Stream ftpStream = response.GetResponseStream();
  200. long cl = response.ContentLength;
  201. int bufferSize = 2048;
  202. int readCount;
  203. byte[] buffer = new byte[bufferSize];
  204. readCount = ftpStream.Read(buffer, 0, bufferSize);
  205. while (readCount > 0)
  206. {
  207. outputStream.Write(buffer, 0, readCount);
  208. readCount = ftpStream.Read(buffer, 0, bufferSize);
  209. }
  210. ftpStream.Close();
  211. outputStream.Close();
  212. response.Close();
  213. }
  214. catch (Exception ex)
  215. {
  216. throw new Exception(ex.Message);
  217. }
  218. }
  219. /// <summary>
  220. /// 下载 filePath是下载到本机的地址fileName是需要下载的文件的名字
  221. /// </summary>
  222. /// <param name="ftpFilePath">FTP下载文件的路径(/mapping/xxx)</param>
  223. /// <param name="savefilePath">本地保存文件路径</param>
  224. /// <param name="savefileName">本地保存文件</param>
  225. /// <param name="isFile">是否是文件</param>
  226. public void DownloadByUrl(string ftpFilePath, string savefilePath, string savefileName, bool isFile = false)
  227. {
  228. try
  229. {
  230. FileStream outputStream = new FileStream(savefilePath + "\\" + savefileName, FileMode.Create);
  231. FtpWebRequest reqFTP;
  232. ftpFilePath = ftpFilePath.Replace("\\", "/");
  233. if (isFile)
  234. {
  235. //DownloadUrl = ftpURI;
  236. DownloadUrl = ftpURI + ftpFilePath;
  237. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(DownloadUrl));
  238. }
  239. else
  240. {
  241. //var url = ftpURI + "//" + ftpFilePath.Trim().TrimStart('\\').TrimStart('/');
  242. DownloadUrl = ftpURI + ftpFilePath;
  243. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(DownloadUrl));
  244. }
  245. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  246. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  247. reqFTP.UseBinary = true;
  248. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  249. Stream ftpStream = response.GetResponseStream();
  250. long cl = response.ContentLength;
  251. int bufferSize = 2048;
  252. int readCount;
  253. byte[] buffer = new byte[bufferSize];
  254. readCount = ftpStream.Read(buffer, 0, bufferSize);
  255. while (readCount > 0)
  256. {
  257. outputStream.Write(buffer, 0, readCount);
  258. readCount = ftpStream.Read(buffer, 0, bufferSize);
  259. }
  260. ftpStream.Close();
  261. outputStream.Close();
  262. response.Close();
  263. }
  264. catch (Exception ex)
  265. {
  266. throw new Exception("DownloadUrl:" + DownloadUrl + ", errorMsg:" + ex.Message);
  267. }
  268. }
  269. /// <summary>
  270. /// 删除服务器的文件 fileName是需要删除的文件的名字
  271. /// </summary>
  272. public void Delete(string fileName)
  273. {
  274. try
  275. {
  276. FtpWebRequest reqFTP;
  277. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
  278. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  279. reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
  280. reqFTP.KeepAlive = false;
  281. string result = String.Empty;
  282. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  283. long size = response.ContentLength;
  284. Stream datastream = response.GetResponseStream();
  285. StreamReader sr = new StreamReader(datastream);
  286. result = sr.ReadToEnd();
  287. sr.Close();
  288. datastream.Close();
  289. response.Close();
  290. }
  291. catch (Exception ex)
  292. {
  293. throw new Exception(ex.Message);
  294. }
  295. }
  296. /// <summary>
  297. /// 获取当前目录下明细(包含文件和文件夹)
  298. /// </summary>
  299. public string[] GetFilesDetailList()
  300. {
  301. try
  302. {
  303. StringBuilder result = new StringBuilder();
  304. FtpWebRequest ftp;
  305. ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
  306. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  307. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  308. WebResponse response = ftp.GetResponse();
  309. StreamReader reader = new StreamReader(response.GetResponseStream());
  310. string line = reader.ReadLine();
  311. line = reader.ReadLine();
  312. line = reader.ReadLine();
  313. while (line != null)
  314. {
  315. result.Append(line);
  316. result.Append("\n");
  317. line = reader.ReadLine();
  318. }
  319. result.Remove(result.ToString().LastIndexOf("\n"), 1);
  320. reader.Close();
  321. response.Close();
  322. return result.ToString().Split('\n');
  323. }
  324. catch (Exception ex)
  325. {
  326. throw new Exception(ex.Message);
  327. }
  328. }
  329. /// <summary>
  330. /// 获取FTP文件列表(包括文件夹)
  331. /// </summary>
  332. private List<string> GetAllList(string url)
  333. {
  334. List<string> list = new List<string>();
  335. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(url));
  336. req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  337. req.Method = WebRequestMethods.Ftp.ListDirectory;
  338. req.UseBinary = true;
  339. req.UsePassive = true;
  340. try
  341. {
  342. using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
  343. {
  344. using (StreamReader sr = new StreamReader(res.GetResponseStream()))
  345. {
  346. string s;
  347. while ((s = sr.ReadLine()) != null)
  348. {
  349. list.Add(s);
  350. }
  351. }
  352. }
  353. }
  354. catch (Exception ex)
  355. {
  356. throw (ex);
  357. }
  358. return list;
  359. }
  360. /// <summary>
  361. /// 获取当前目录下文件列表(不包括文件夹)
  362. /// </summary>
  363. public List<string> GetFileList(string url)
  364. {
  365. var fileNameList = new List<string>();
  366. //StringBuilder result = new StringBuilder();
  367. FtpWebRequest reqFTP;
  368. try
  369. {
  370. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  371. reqFTP.UseBinary = true;
  372. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  373. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  374. WebResponse response = reqFTP.GetResponse();
  375. StreamReader reader = new StreamReader(response.GetResponseStream());
  376. string line = reader.ReadLine();
  377. while (line != null)
  378. {
  379. //rwxrwxr-- 1 1001 1001 4520 Oct 31 2023 AD2300225P1.xml
  380. CommonDefine.SaveWorkLogs("GetFileList() " + line);
  381. //if (line.IndexOf("<DIR>") == -1)
  382. //{
  383. // var fileName = Regex.Match(line, @"[\S]+ [\S]+", RegexOptions.IgnoreCase).Value.Split(' ')[1];
  384. // CommonDefine.SaveWorkLogs("GetFileList()1 fileName:" + fileName);
  385. // result.Append(fileName);
  386. // result.Append("\n");
  387. //}
  388. var textList = line.Split(' ').ToList();
  389. var fileName = textList.LastOrDefault();
  390. //result.Append(fileName);
  391. fileNameList.Add(fileName);
  392. line = reader.ReadLine();
  393. }
  394. //result.Remove(result.ToString().LastIndexOf('\n'), 1);
  395. reader.Close();
  396. response.Close();
  397. }
  398. catch (Exception ex)
  399. {
  400. //throw (ex);
  401. throw new Exception(ex.Message + ",url:" + url + ",ftpUserID:" + ftpUserID + ",ftpPassword:" + ftpPassword);
  402. }
  403. //return result.ToString().Split('\n');
  404. return fileNameList;
  405. }
  406. /// <summary>
  407. /// 获取当前目录下文件列表(不包括文件夹)
  408. /// </summary>
  409. public List<string> GetFileListByFilePath(string filePath)
  410. {
  411. var fileList = new List<string>();
  412. //var url = ftpURI + "//" + filePath.Trim().TrimStart('\\').TrimStart('/');
  413. var url = ftpURI + filePath.Trim();
  414. try
  415. {
  416. //var fileNameArray = GetFileList(ftpURI + "//" + filePath);
  417. var fileNameArray = GetFileList(url);
  418. if (fileNameArray != null && fileNameArray.Count > 0)
  419. {
  420. fileList = fileNameArray;
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. //throw (ex);
  426. throw new Exception("url:" + url + ",errormsg:" + ex.Message);
  427. }
  428. return fileList;
  429. }
  430. /// <summary>
  431. /// 判断当前目录下指定的文件是否存在
  432. /// </summary>
  433. /// <param name="RemoteFileName">远程文件名</param>
  434. public bool FileExist(string RemoteFileName)
  435. {
  436. var fileList = GetFileList("*.*");
  437. foreach (string str in fileList)
  438. {
  439. if (str.Trim() == RemoteFileName.Trim())
  440. {
  441. return true;
  442. }
  443. }
  444. return false;
  445. }
  446. /// <summary>
  447. /// 创建文件夹
  448. /// </summary>
  449. public CommonResultModel<string> MakeDir(string ftpDirName)
  450. {
  451. var resultModel = new CommonResultModel<string>();
  452. FtpWebRequest reqFTP;
  453. FtpWebResponse response = null;
  454. try
  455. {
  456. //resultModel.Msg = "MakeDir 1";
  457. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpDirName));
  458. //resultModel.Msg = "MakeDir 2";
  459. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;//创建文件夹
  460. reqFTP.UseBinary = true;
  461. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  462. //resultModel.Msg = "MakeDir 3";
  463. using (response = (FtpWebResponse)reqFTP.GetResponse())
  464. {
  465. //resultModel.Msg = "MakeDir 4";
  466. Stream ftpStream = response.GetResponseStream();
  467. //resultModel.Msg = "MakeDir 5";
  468. ftpStream.Close();
  469. resultModel.IsSucceed = true;
  470. }
  471. //response.Close();
  472. }
  473. catch (Exception ex)
  474. {
  475. //resultModel.Msg = ex.Message;
  476. throw ex;
  477. }
  478. finally
  479. {
  480. if (response != null)
  481. {
  482. response.Close();
  483. }
  484. }
  485. return resultModel;
  486. }
  487. /// <summary>
  488. /// 判断文件夹是否存在
  489. /// </summary>
  490. /// <param name="ftpServer"></param>
  491. /// <param name="ftpFolder">文件夹</param>
  492. /// <param name="ftpUsername"></param>
  493. /// <param name="ftpPassword"></param>
  494. /// <returns></returns>
  495. public bool FolderExists(string ftpFolder, string findFolder)
  496. {
  497. bool folderExists = false;
  498. string uploadUrl = string.Empty;
  499. try
  500. {
  501. var fileNameList = CommonData.FtpServer_MappingFile.GetAllList(ftpFolder.Trim());
  502. foreach (var item in fileNameList)
  503. {
  504. //this.lbFtpFileNames.Items.Add(item);
  505. //CommonDefine.SaveWorkLogs("判断文件夹是否存在 获取FTP所有文件 " + ftpFolder + ":" + item);
  506. if (item.Contains(findFolder))
  507. {
  508. folderExists = true;
  509. //CommonDefine.SaveWorkLogs("判断文件夹是否存在 获取FTP所有文件 " + ftpFolder + ":" + item + "," + findFolder + "目录存在");
  510. break;
  511. }
  512. }
  513. }
  514. catch (WebException ex)
  515. {
  516. var response = (FtpWebResponse)ex.Response;
  517. if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
  518. {
  519. folderExists = false;
  520. }
  521. else
  522. {
  523. // 处理其他异常
  524. }
  525. }
  526. return folderExists;
  527. }
  528. /// <summary>
  529. /// 获取指定文件大小
  530. /// </summary>
  531. public long GetFileSize(string filename)
  532. {
  533. FtpWebRequest reqFTP;
  534. long fileSize = 0;
  535. try
  536. {
  537. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
  538. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  539. reqFTP.UseBinary = true;
  540. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  541. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  542. Stream ftpStream = response.GetResponseStream();
  543. fileSize = response.ContentLength;
  544. ftpStream.Close();
  545. response.Close();
  546. }
  547. catch (Exception ex)
  548. { }
  549. return fileSize;
  550. }
  551. /// <summary>
  552. /// 更改文件名
  553. /// </summary>
  554. public void ReName(string currentFilename, string newFilename)
  555. {
  556. FtpWebRequest reqFTP;
  557. try
  558. {
  559. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));
  560. reqFTP.Method = WebRequestMethods.Ftp.Rename;
  561. reqFTP.RenameTo = newFilename;
  562. reqFTP.UseBinary = true;
  563. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  564. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  565. Stream ftpStream = response.GetResponseStream();
  566. ftpStream.Close();
  567. response.Close();
  568. }
  569. catch (Exception ex)
  570. { }
  571. }
  572. /// <summary>
  573. /// 移动文件
  574. /// </summary>
  575. public void MovieFile(string currentFilename, string newDirectory)
  576. {
  577. ReName(currentFilename, newDirectory);
  578. }
  579. /// <summary>
  580. /// 切换当前目录
  581. /// </summary>
  582. /// <param name="IsRoot">true:绝对路径 false:相对路径</param>
  583. public void GotoDirectory(string DirectoryName, bool IsRoot)
  584. {
  585. if (IsRoot)
  586. {
  587. ftpRemotePath = DirectoryName;
  588. }
  589. else
  590. {
  591. ftpRemotePath += DirectoryName + "/";
  592. }
  593. ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
  594. }
  595. /// <summary>
  596. /// 删除ftpURI目录下指定的文件夹
  597. /// </summary>
  598. public void DeleteDir(string dirName)
  599. {
  600. FtpWebRequest reqFTP;
  601. try
  602. {
  603. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
  604. reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
  605. reqFTP.UseBinary = true;
  606. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  607. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  608. Stream ftpStream = response.GetResponseStream();
  609. ftpStream.Close();
  610. response.Close();
  611. }
  612. catch (Exception ex)
  613. {
  614. throw new Exception(ex.Message);
  615. }
  616. }
  617. }

本文转载自: https://blog.csdn.net/cjh16606260986/article/details/143358753
版权归原作者 StevenChen85 所有, 如有侵权,请联系我们删除。

“C# ftp帮助类 项目实战优化版”的评论:

还没有评论