1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Net; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace ManagementProject 10 { 11 public class FTPHelper 12 { 13 string ftpRemotePath; 14 15 #region 变量属性 16 /// <summary> 17 /// Ftp服务器ip 18 /// </summary> 19 public static string FtpServerIP = ""; 20 /// <summary> 21 /// Ftp 指定用户名 22 /// </summary> 23 public static string FtpUserID = ""; 24 /// <summary> 25 /// Ftp 指定用户密码 26 /// </summary> 27 public static string FtpPassword = ""; 28 29 public static string ftpURI = "ftp://" + FtpServerIP + "/"; 30 31 #endregion 32 33 #region 从FTP服务器下载文件,指定本地路径和本地文件名 34 /// <summary> 35 /// 从FTP服务器下载文件,指定本地路径和本地文件名 36 /// </summary> 37 /// <param name="remoteFileName">远程文件名</param> 38 /// <param name="localFileName">保存本地的文件名(包含路径)</param> 39 /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> 40 /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> 41 /// <returns>是否下载成功</returns> 42 public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null) 43 { 44 FtpWebRequest reqFTP, ftpsize; 45 Stream ftpStream = null; 46 FtpWebResponse response = null; 47 FileStream outputStream = null; 48 try 49 { 50 51 outputStream = new FileStream(localFileName, FileMode.Create); 52 if (FtpServerIP == null || FtpServerIP.Trim().Length == 0) 53 { 54 throw new Exception("ftp下载目标服务器地址未设置!"); 55 } 56 Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName); 57 ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri); 58 ftpsize.UseBinary = true; 59 60 reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); 61 reqFTP.UseBinary = true; 62 reqFTP.KeepAlive = false; 63 if (ifCredential)//使用用户身份认证 64 { 65 ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 66 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 67 } 68 ftpsize.Method = WebRequestMethods.Ftp.GetFileSize; 69 FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse(); 70 long totalBytes = re.ContentLength; 71 re.Close(); 72 73 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 74 response = (FtpWebResponse)reqFTP.GetResponse(); 75 ftpStream = response.GetResponseStream(); 76 77 //更新进度 78 if (updateProgress != null) 79 { 80 updateProgress((int)totalBytes, 0);//更新进度条 81 } 82 long totalDownloadedByte = 0; 83 int bufferSize = 2048; 84 int readCount; 85 byte[] buffer = new byte[bufferSize]; 86 readCount = ftpStream.Read(buffer, 0, bufferSize); 87 while (readCount > 0) 88 { 89 totalDownloadedByte = readCount + totalDownloadedByte; 90 outputStream.Write(buffer, 0, readCount); 91 //更新进度 92 if (updateProgress != null) 93 { 94 updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条 95 } 96 readCount = ftpStream.Read(buffer, 0, bufferSize); 97 } 98 ftpStream.Close(); 99 outputStream.Close(); 100 response.Close(); 101 return true; 102 } 103 catch (Exception ex) 104 { 105 return false; 106 throw; 107 } 108 finally 109 { 110 if (ftpStream != null) 111 { 112 ftpStream.Close(); 113 } 114 if (outputStream != null) 115 { 116 outputStream.Close(); 117 } 118 if (response != null) 119 { 120 response.Close(); 121 } 122 } 123 } 124 /// <summary> 125 /// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载) 126 /// </summary> 127 /// <param name="remoteFileName">远程文件名</param> 128 /// <param name="localFileName">保存本地的文件名(包含路径)</param> 129 /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> 130 /// <param name="size">已下载文件流大小</param> 131 /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> 132 /// <returns>是否下载成功</returns> 133 public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null) 134 { 135 FtpWebRequest reqFTP, ftpsize; 136 Stream ftpStream = null; 137 FtpWebResponse response = null; 138 FileStream outputStream = null; 139 try 140 { 141 142 outputStream = new FileStream(localFileName, FileMode.Append); 143 if (FtpServerIP == null || FtpServerIP.Trim().Length == 0) 144 { 145 throw new Exception("ftp下载目标服务器地址未设置!"); 146 } 147 Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName); 148 ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri); 149 ftpsize.UseBinary = true; 150 ftpsize.ContentOffset = size; 151 152 reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); 153 reqFTP.UseBinary = true; 154 reqFTP.KeepAlive = false; 155 reqFTP.ContentOffset = size; 156 if (ifCredential)//使用用户身份认证 157 { 158 ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 159 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 160 } 161 ftpsize.Method = WebRequestMethods.Ftp.GetFileSize; 162 FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse(); 163 long totalBytes = re.ContentLength; 164 re.Close(); 165 166 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 167 response = (FtpWebResponse)reqFTP.GetResponse(); 168 ftpStream = response.GetResponseStream(); 169 170 //更新进度 171 if (updateProgress != null) 172 { 173 updateProgress((int)totalBytes, 0);//更新进度条 174 } 175 long totalDownloadedByte = 0; 176 int bufferSize = 2048; 177 int readCount; 178 byte[] buffer = new byte[bufferSize]; 179 readCount = ftpStream.Read(buffer, 0, bufferSize); 180 while (readCount > 0) 181 { 182 totalDownloadedByte = readCount + totalDownloadedByte; 183 outputStream.Write(buffer, 0, readCount); 184 //更新进度 185 if (updateProgress != null) 186 { 187 updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条 188 } 189 readCount = ftpStream.Read(buffer, 0, bufferSize); 190 } 191 ftpStream.Close(); 192 outputStream.Close(); 193 response.Close(); 194 return true; 195 } 196 catch (Exception ex) 197 { 198 return false; 199 throw; 200 } 201 finally 202 { 203 if (ftpStream != null) 204 { 205 ftpStream.Close(); 206 } 207 if (outputStream != null) 208 { 209 outputStream.Close(); 210 } 211 if (response != null) 212 { 213 response.Close(); 214 } 215 } 216 } 217 218 /// <summary> 219 /// 从FTP服务器下载文件,指定本地路径和本地文件名 220 /// </summary> 221 /// <param name="remoteFileName">远程文件名</param> 222 /// <param name="localFileName">保存本地的文件名(包含路径)</param> 223 /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> 224 /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> 225 /// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param> 226 /// <returns>是否下载成功</returns> 227 public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null) 228 { 229 if (brokenOpen) 230 { 231 try 232 { 233 long size = 0; 234 if (File.Exists(localFileName)) 235 { 236 using (FileStream outputStream = new FileStream(localFileName, FileMode.Open)) 237 { 238 size = outputStream.Length; 239 } 240 } 241 return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress); 242 } 243 catch 244 { 245 throw; 246 } 247 } 248 else 249 { 250 return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress); 251 } 252 } 253 #endregion 254 255 #region 上传文件到FTP服务器 256 /// <summary> 257 /// 上传文件到FTP服务器 258 /// </summary> 259 /// <param name="localFullPath">本地带有完整路径的文件名</param> 260 /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> 261 /// <returns>是否下载成功</returns> 262 public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null) 263 { 264 FtpWebRequest reqFTP; 265 Stream stream = null; 266 FtpWebResponse response = null; 267 FileStream fs = null; 268 try 269 { 270 FileInfo finfo = new FileInfo(localFullPathName); 271 if (FtpServerIP == null || FtpServerIP.Trim().Length == 0) 272 { 273 throw new Exception("ftp上传目标服务器地址未设置!"); 274 } 275 Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name); 276 reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); 277 reqFTP.KeepAlive = false; 278 reqFTP.UseBinary = true; 279 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码 280 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令 281 reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小 282 response = reqFTP.GetResponse() as FtpWebResponse; 283 reqFTP.ContentLength = finfo.Length; 284 int buffLength = 1024; 285 byte[] buff = new byte[buffLength]; 286 int contentLen; 287 fs = finfo.OpenRead(); 288 stream = reqFTP.GetRequestStream(); 289 contentLen = fs.Read(buff, 0, buffLength); 290 int allbye = (int)finfo.Length; 291 //更新进度 292 if (updateProgress != null) 293 { 294 updateProgress((int)allbye, 0);//更新进度条 295 } 296 int startbye = 0; 297 while (contentLen != 0) 298 { 299 startbye = contentLen + startbye; 300 stream.Write(buff, 0, contentLen); 301 //更新进度 302 if (updateProgress != null) 303 { 304 updateProgress((int)allbye, (int)startbye);//更新进度条 305 } 306 contentLen = fs.Read(buff, 0, buffLength); 307 } 308 stream.Close(); 309 fs.Close(); 310 response.Close(); 311 return true; 312 313 } 314 catch (Exception ex) 315 { 316 return false; 317 throw; 318 } 319 finally 320 { 321 if (fs != null) 322 { 323 fs.Close(); 324 } 325 if (stream != null) 326 { 327 stream.Close(); 328 } 329 if (response != null) 330 { 331 response.Close(); 332 } 333 } 334 } 335 336 /// <summary> 337 /// 上传文件到FTP服务器(断点续传) 338 /// </summary> 339 /// <param name="localFullPath">本地文件全路径名称:C:UsersJianKunKingDesktopIronPython脚本测试工具</param> 340 /// <param name="remoteFilepath">远程文件所在文件夹路径</param> 341 /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> 342 /// <returns></returns> 343 public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null) 344 { 345 if (remoteFilepath == null) 346 { 347 remoteFilepath = ""; 348 } 349 string newFileName = string.Empty; 350 bool success = true; 351 FileInfo fileInf = new FileInfo(localFullPath); 352 long allbye = (long)fileInf.Length; 353 if (fileInf.Name.IndexOf("#") == -1) 354 { 355 newFileName = RemoveSpaces(fileInf.Name); 356 } 357 else 358 { 359 newFileName = fileInf.Name.Replace("#", "#"); 360 newFileName = RemoveSpaces(newFileName); 361 } 362 long startfilesize = GetFileSize(newFileName, remoteFilepath); 363 if (startfilesize >= allbye) 364 { 365 return false; 366 } 367 long startbye = startfilesize; 368 //更新进度 369 if (updateProgress != null) 370 { 371 updateProgress((int)allbye, (int)startfilesize);//更新进度条 372 } 373 374 string uri; 375 if (remoteFilepath.Length == 0) 376 { 377 uri = "ftp://" + FtpServerIP + "/" + newFileName; 378 } 379 else 380 { 381 uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + newFileName; 382 } 383 FtpWebRequest reqFTP; 384 // 根据uri创建FtpWebRequest对象 385 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 386 // ftp用户名和密码 387 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 388 // 默认为true,连接不会被关闭 389 // 在一个命令之后被执行 390 reqFTP.KeepAlive = false; 391 // 指定执行什么命令 392 reqFTP.Method = WebRequestMethods.Ftp.AppendFile; 393 // 指定数据传输类型 394 reqFTP.UseBinary = true; 395 // 上传文件时通知服务器文件的大小 396 reqFTP.ContentLength = fileInf.Length; 397 int buffLength = 2048;// 缓冲大小设置为2kb 398 byte[] buff = new byte[buffLength]; 399 // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 400 FileStream fs = fileInf.OpenRead(); 401 Stream strm = null; 402 try 403 { 404 // 把上传的文件写入流 405 strm = reqFTP.GetRequestStream(); 406 // 每次读文件流的2kb 407 fs.Seek(startfilesize, 0); 408 int contentLen = fs.Read(buff, 0, buffLength); 409 // 流内容没有结束 410 while (contentLen != 0) 411 { 412 // 把内容从file stream 写入 upload stream 413 strm.Write(buff, 0, contentLen); 414 contentLen = fs.Read(buff, 0, buffLength); 415 startbye += contentLen; 416 //更新进度 417 if (updateProgress != null) 418 { 419 updateProgress((int)allbye, (int)startbye);//更新进度条 420 } 421 } 422 // 关闭两个流 423 strm.Close(); 424 fs.Close(); 425 } 426 catch 427 { 428 success = false; 429 throw; 430 } 431 finally 432 { 433 if (fs != null) 434 { 435 fs.Close(); 436 } 437 if (strm != null) 438 { 439 strm.Close(); 440 } 441 } 442 return success; 443 } 444 445 /// <summary> 446 /// 去除空格 447 /// </summary> 448 /// <param name="str"></param> 449 /// <returns></returns> 450 private static string RemoveSpaces(string str) 451 { 452 string a = ""; 453 CharEnumerator CEnumerator = str.GetEnumerator(); 454 while (CEnumerator.MoveNext()) 455 { 456 byte[] array = new byte[1]; 457 array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString()); 458 int asciicode = (short)(array[0]); 459 if (asciicode != 32) 460 { 461 a += CEnumerator.Current.ToString(); 462 } 463 } 464 string sdate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() 465 + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString(); 466 return a.Split('.')[a.Split('.').Length - 2] + "." + a.Split('.')[a.Split('.').Length - 1]; 467 } 468 /// <summary> 469 /// 获取已上传文件大小 470 /// </summary> 471 /// <param name="filename">文件名称</param> 472 /// <param name="path">服务器文件路径</param> 473 /// <returns></returns> 474 public static long GetFileSize(string filename, string remoteFilepath) 475 { 476 long filesize = 0; 477 try 478 { 479 FtpWebRequest reqFTP; 480 FileInfo fi = new FileInfo(filename); 481 string uri; 482 if (remoteFilepath.Length == 0) 483 { 484 uri = "ftp://" + FtpServerIP + "/" + fi.Name; 485 } 486 else 487 { 488 uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + fi.Name; 489 } 490 reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); 491 reqFTP.KeepAlive = false; 492 reqFTP.UseBinary = true; 493 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码 494 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; 495 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 496 filesize = response.ContentLength; 497 return filesize; 498 } 499 catch 500 { 501 return 0; 502 } 503 } 504 505 //public void Connect(String path, string ftpUserID, string ftpPassword)//连接ftp 506 //{ 507 // // 根据uri创建FtpWebRequest对象 508 // reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); 509 // // 指定数据传输类型 510 // reqFTP.UseBinary = true; 511 // // ftp用户名和密码 512 // reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 513 //} 514 515 #endregion 516 517 #region 获取当前目录下明细 518 /// <summary> 519 /// 获取当前目录下明细(包含文件和文件夹) 520 /// </summary> 521 /// <returns></returns> 522 public static string[] GetFilesDetailList() 523 { 524 string[] downloadFiles; 525 try 526 { 527 StringBuilder result = new StringBuilder(); 528 FtpWebRequest ftp; 529 ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI)); 530 ftp.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 531 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 532 WebResponse response = ftp.GetResponse(); 533 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); 534 string line = reader.ReadLine(); 535 536 while (line != null) 537 { 538 result.Append(line); 539 result.Append(" "); 540 line = reader.ReadLine(); 541 } 542 result.Remove(result.ToString().LastIndexOf(" "), 1); 543 reader.Close(); 544 response.Close(); 545 return result.ToString().Split(' '); 546 } 547 catch (Exception ex) 548 { 549 downloadFiles = null; 550 throw ex; 551 } 552 } 553 554 /// <summary> 555 /// 获取当前目录下文件列表(仅文件) 556 /// </summary> 557 /// <returns></returns> 558 public static string[] GetFileList(string mask) 559 { 560 string[] downloadFiles; 561 StringBuilder result = new StringBuilder(); 562 FtpWebRequest reqFTP; 563 try 564 { 565 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI)); 566 reqFTP.UseBinary = true; 567 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 568 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 569 WebResponse response = reqFTP.GetResponse(); 570 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); 571 572 string line = reader.ReadLine(); 573 while (line != null) 574 { 575 if (mask.Trim() != string.Empty && mask.Trim() != "*.*") 576 { 577 578 string mask_ = mask.Substring(0, mask.IndexOf("*")); 579 if (line.Substring(0, mask_.Length) == mask_) 580 { 581 result.Append(line); 582 result.Append(" "); 583 } 584 } 585 else 586 { 587 result.Append(line); 588 result.Append(" "); 589 } 590 line = reader.ReadLine(); 591 } 592 result.Remove(result.ToString().LastIndexOf(' '), 1); 593 reader.Close(); 594 response.Close(); 595 return result.ToString().Split(' '); 596 } 597 catch (Exception ex) 598 { 599 downloadFiles = null; 600 throw ex; 601 } 602 } 603 604 /// <summary> 605 /// 获取当前目录下所有的文件夹列表(仅文件夹) 606 /// </summary> 607 /// <returns></returns> 608 public static string[] GetDirectoryList() 609 { 610 string[] drectory = GetFilesDetailList(); 611 string m = string.Empty; 612 foreach (string str in drectory) 613 { 614 int dirPos = str.IndexOf("<DIR>"); 615 if (dirPos > 0) 616 { 617 /*判断 Windows 风格*/ 618 m += str.Substring(dirPos + 5).Trim() + " "; 619 } 620 else if (str.Trim().Substring(0, 1).ToUpper() == "D") 621 { 622 /*判断 Unix 风格*/ 623 string dir = str.Substring(54).Trim(); 624 if (dir != "." && dir != "..") 625 { 626 m += dir + " "; 627 } 628 } 629 } 630 631 char[] n = new char[] { ' ' }; 632 return m.Split(n); 633 } 634 #endregion 635 636 #region 删除文件及文件夹 637 /// <summary> 638 /// 删除文件 639 /// </summary> 640 /// <param name="fileName"></param> 641 public static bool Delete(string fileName) 642 { 643 try 644 { 645 string uri = ftpURI + fileName; 646 FtpWebRequest reqFTP; 647 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 648 649 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 650 reqFTP.KeepAlive = false; 651 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; 652 653 string result = String.Empty; 654 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 655 long size = response.ContentLength; 656 Stream datastream = response.GetResponseStream(); 657 StreamReader sr = new StreamReader(datastream); 658 result = sr.ReadToEnd(); 659 sr.Close(); 660 datastream.Close(); 661 response.Close(); 662 return true; 663 } 664 catch (Exception ex) 665 { 666 return false; 667 throw ex; 668 } 669 } 670 671 /// <summary> 672 /// 删除文件夹 673 /// </summary> 674 /// <param name="folderName"></param> 675 public static void RemoveDirectory(string folderName) 676 { 677 try 678 { 679 string uri = ftpURI + folderName; 680 FtpWebRequest reqFTP; 681 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 682 683 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 684 reqFTP.KeepAlive = false; 685 reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory; 686 687 string result = String.Empty; 688 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 689 long size = response.ContentLength; 690 Stream datastream = response.GetResponseStream(); 691 StreamReader sr = new StreamReader(datastream); 692 result = sr.ReadToEnd(); 693 sr.Close(); 694 datastream.Close(); 695 response.Close(); 696 } 697 catch (Exception ex) 698 { 699 throw ex; 700 } 701 } 702 #endregion 703 704 #region 其他操作 705 /// <summary> 706 /// 获取指定文件大小 707 /// </summary> 708 /// <param name="filename"></param> 709 /// <returns></returns> 710 public static long GetFileSize(string filename) 711 { 712 FtpWebRequest reqFTP; 713 long fileSize = 0; 714 try 715 { 716 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename)); 717 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; 718 reqFTP.UseBinary = true; 719 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 720 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 721 Stream ftpStream = response.GetResponseStream(); 722 fileSize = response.ContentLength; 723 724 ftpStream.Close(); 725 response.Close(); 726 } 727 catch (Exception ex) 728 { 729 throw ex; 730 } 731 return fileSize; 732 } 733 734 /// <summary> 735 /// 判断当前目录下指定的子目录是否存在 736 /// </summary> 737 /// <param name="RemoteDirectoryName">指定的目录名</param> 738 public bool DirectoryExist(string RemoteDirectoryName) 739 { 740 try 741 { 742 string[] dirList = GetDirectoryList(); 743 744 foreach (string str in dirList) 745 { 746 if (str.Trim() == RemoteDirectoryName.Trim()) 747 { 748 return true; 749 } 750 } 751 return false; 752 } 753 catch 754 { 755 return false; 756 } 757 758 } 759 760 /// <summary> 761 /// 判断当前目录下指定的文件是否存在 762 /// </summary> 763 /// <param name="RemoteFileName">远程文件名</param> 764 public bool FileExist(string RemoteFileName) 765 { 766 string[] fileList = GetFileList("*.*"); 767 foreach (string str in fileList) 768 { 769 if (str.Trim() == RemoteFileName.Trim()) 770 { 771 return true; 772 } 773 } 774 return false; 775 } 776 777 /// <summary> 778 /// 创建文件夹 779 /// </summary> 780 /// <param name="dirName"></param> 781 public void MakeDir(string dirName) 782 { 783 FtpWebRequest reqFTP; 784 try 785 { 786 // dirName = name of the directory to create. 787 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName)); 788 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; 789 reqFTP.UseBinary = true; 790 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 791 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 792 Stream ftpStream = response.GetResponseStream(); 793 794 ftpStream.Close(); 795 response.Close(); 796 } 797 catch (Exception ex) 798 { 799 throw ex; 800 } 801 } 802 803 /// <summary> 804 /// 改名 805 /// </summary> 806 /// <param name="currentFilename"></param> 807 /// <param name="newFilename"></param> 808 public void ReName(string currentFilename, string newFilename) 809 { 810 FtpWebRequest reqFTP; 811 try 812 { 813 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename)); 814 reqFTP.Method = WebRequestMethods.Ftp.Rename; 815 reqFTP.RenameTo = newFilename; 816 reqFTP.UseBinary = true; 817 reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); 818 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 819 Stream ftpStream = response.GetResponseStream(); 820 821 ftpStream.Close(); 822 response.Close(); 823 } 824 catch (Exception ex) 825 { 826 throw ex; 827 } 828 } 829 830 /// <summary> 831 /// 移动文件 832 /// </summary> 833 /// <param name="currentFilename"></param> 834 /// <param name="newFilename"></param> 835 public void MovieFile(string currentFilename, string newDirectory) 836 { 837 ReName(currentFilename, newDirectory); 838 } 839 840 /// <summary> 841 /// 切换当前目录 842 /// </summary> 843 /// <param name="DirectoryName"></param> 844 /// <param name="IsRoot">true 绝对路径 false 相对路径</param> 845 public void GotoDirectory(string DirectoryName, bool IsRoot) 846 { 847 848 if (IsRoot) 849 { 850 ftpRemotePath = DirectoryName; 851 } 852 else 853 { 854 ftpRemotePath += DirectoryName + "/"; 855 } 856 ftpURI = "ftp://" + FtpServerIP + "/" + ftpRemotePath + "/"; 857 } 858 #endregion 859 860 861 } 862 }