C#FTP方式文件上传远程服务器

来源:互联网 发布:友谦网络招聘 编辑:程序博客网 时间:2024/05/29 17:28

C#实现客户端与服务器端的文件传输

          实现文件的异地传输,可以采用http协议  ftp等协议;ftp传输的原理同样也是数据流的IO数据交换。关键的4步有:1.客户端与服务端的连接;2.客户端的文件流转换;
3.I/O数据传输;4.服务端数据的写入.


  1. public class FtpWeb  
  2. {  
  3.     string ftpRemotePath;  
  4.     string ftpUserID;  
  5.     string ftpPassword;  
  6.     string ftpURI;  
  7.     string ftpServerIP;  
  8.   
  9.     /// <summary>  
  10.     /// 连接FTP  
  11.     /// </summary>  
  12.     /// <param name="FtpServerIP">FTP连接地址</param>  
  13.     /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>  
  14.     /// <param name="FtpUserID">用户名</param>  
  15.     /// <param name="FtpPassword">密码</param>  
  16.     public FtpWeb(string FtpServerIP, string FtpUserID, string FtpPassword)  
  17.     {  
  18.         ftpServerIP = FtpServerIP;  
  19.         ftpUserID = FtpUserID;  
  20.         ftpPassword = FtpPassword;  
  21.         ftpURI = FtpServerIP + "/";  
  22.     }  
  23.   
  24.     /// <summary>  
  25.     /// 上传  
  26.     /// </summary>  
  27.     /// <param name="filename"></param>  
  28.     public void Upload(string filename)  
  29.     {  
  30.         FileInfo fileInf = new FileInfo(filename);  
  31.         string uri = ftpURI + fileInf.Name;  
  32.         FtpWebRequest reqFTP;  
  33.   
  34.         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  35.         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  36.         reqFTP.KeepAlive = false;  
  37.         reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  38.         reqFTP.UseBinary = true;  
  39.         reqFTP.ContentLength = fileInf.Length;  
  40.         int buffLength = 2048;  
  41.         byte[] buff = new byte[buffLength];  
  42.         int contentLen;  
  43.         FileStream fs = fileInf.OpenRead();  
  44.         try  
  45.         {  
  46.             Stream strm = reqFTP.GetRequestStream();  
  47.             contentLen = fs.Read(buff, 0, buffLength);  
  48.             while (contentLen != 0)  
  49.             {  
  50.                 strm.Write(buff, 0, contentLen);  
  51.                 contentLen = fs.Read(buff, 0, buffLength);  
  52.             }  
  53.             strm.Close();  
  54.             fs.Close();  
  55.         }  
  56.         catch (Exception ex)  
  57.         {  
  58.             throw ex;  
  59.         }  
  60.     }  
  61.     /// <summary>  
  62.     /// 上传  
  63.     /// </summary>  
  64.     /// <param name="filename"></param>  
  65.     public void Upload2(string ftpuri, string filename)  
  66.     {  
  67.         FileInfo fileInf = new FileInfo(filename);  
  68.         string uri = ftpuri;  
  69.         FtpWebRequest reqFTP;  
  70.   
  71.         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  72.         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  73.         reqFTP.KeepAlive = false;  
  74.         reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  75.         reqFTP.UseBinary = true;  
  76.         reqFTP.ContentLength = fileInf.Length;  
  77.         int buffLength = 2048;  
  78.         byte[] buff = new byte[buffLength];  
  79.         int contentLen;  
  80.         FileStream fs = fileInf.OpenRead();  
  81.         try  
  82.         {  
  83.             Stream strm = reqFTP.GetRequestStream();  
  84.             contentLen = fs.Read(buff, 0, buffLength);  
  85.             while (contentLen != 0)  
  86.             {  
  87.                 strm.Write(buff, 0, contentLen);  
  88.                 contentLen = fs.Read(buff, 0, buffLength);  
  89.             }  
  90.             strm.Close();  
  91.             fs.Close();  
  92.         }  
  93.         catch (Exception ex)  
  94.         {  
  95.             throw ex;  
  96.         }  
  97.     }  
  98.     /// <summary>  
  99.     /// 下载  
  100.     /// </summary>  
  101.     /// <param name="filePath"></param>  
  102.     /// <param name="fileName"></param>  
  103.     public void Download(string filePath, string ftpUri, string fileName)  
  104.     {  
  105.         FtpWebRequest reqFTP;  
  106.         try  
  107.         {  
  108.             FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);  
  109.   
  110.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUri));  
  111.             reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
  112.             reqFTP.UseBinary = true;  
  113.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  114.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  115.             Stream ftpStream = response.GetResponseStream();  
  116.             long cl = response.ContentLength;  
  117.             int bufferSize = 2048;  
  118.             int readCount;  
  119.             byte[] buffer = new byte[bufferSize];  
  120.   
  121.             readCount = ftpStream.Read(buffer, 0, bufferSize);  
  122.             while (readCount > 0)  
  123.             {  
  124.                 outputStream.Write(buffer, 0, readCount);  
  125.                 readCount = ftpStream.Read(buffer, 0, bufferSize);  
  126.             }  
  127.   
  128.             ftpStream.Close();  
  129.             outputStream.Close();  
  130.             response.Close();  
  131.         }  
  132.         catch (Exception ex)  
  133.         {  
  134.             throw ex;  
  135.         }  
  136.     }  
  137.   
  138.   
  139.     /// <summary>  
  140.     /// 删除文件  
  141.     /// </summary>  
  142.     /// <param name="fileName"></param>  
  143.     public void Delete(string fileName)  
  144.     {  
  145.         try  
  146.         {  
  147.             string uri = fileName;  
  148.             FtpWebRequest reqFTP;  
  149.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  150.   
  151.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  152.             reqFTP.KeepAlive = false;  
  153.             reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;  
  154.   
  155.             string result = String.Empty;  
  156.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  157.             long size = response.ContentLength;  
  158.             Stream datastream = response.GetResponseStream();  
  159.             StreamReader sr = new StreamReader(datastream);  
  160.             result = sr.ReadToEnd();  
  161.             sr.Close();  
  162.             datastream.Close();  
  163.             response.Close();  
  164.         }  
  165.         catch (Exception ex)  
  166.         {  
  167.             throw ex;  
  168.         }  
  169.     }  
  170.   
  171.     /// <summary>  
  172.     /// 删除文件夹  
  173.     /// </summary>  
  174.     /// <param name="folderName"></param>  
  175.     public void RemoveDirectory(string folderName)  
  176.     {  
  177.         try  
  178.         {  
  179.             string uri = ftpURI + folderName;  
  180.             FtpWebRequest reqFTP;  
  181.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  182.   
  183.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  184.             reqFTP.KeepAlive = false;  
  185.             reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;  
  186.   
  187.             string result = String.Empty;  
  188.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  189.             long size = response.ContentLength;  
  190.             Stream datastream = response.GetResponseStream();  
  191.             StreamReader sr = new StreamReader(datastream);  
  192.             result = sr.ReadToEnd();  
  193.             sr.Close();  
  194.             datastream.Close();  
  195.             response.Close();  
  196.         }  
  197.         catch (Exception ex)  
  198.         {  
  199.             throw ex;  
  200.         }  
  201.     }  
  202.   
  203.     /// <summary>  
  204.     /// 获取当前目录下明细(包含文件和文件夹)  
  205.     /// </summary>  
  206.     /// <returns></returns>  
  207.     public string[] GetFilesDetailList()  
  208.     {  
  209.         string[] downloadFiles;  
  210.         try  
  211.         {  
  212.             StringBuilder result = new StringBuilder();  
  213.             FtpWebRequest ftp;  
  214.             ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));  
  215.             ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  216.             ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;  
  217.             WebResponse response = ftp.GetResponse();  
  218.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  219.             string line = reader.ReadLine();  
  220.   
  221.             while (line != null)  
  222.             {  
  223.                 result.Append(line);  
  224.                 result.Append("\n");  
  225.                 line = reader.ReadLine();  
  226.             }  
  227.             result.Remove(result.ToString().LastIndexOf("\n"), 1);  
  228.             reader.Close();  
  229.             response.Close();  
  230.             return result.ToString().Split('\n');  
  231.         }  
  232.         catch (Exception ex)  
  233.         {  
  234.             downloadFiles = null;  
  235.             throw ex;  
  236.         }  
  237.     }  
  238.   
  239.     /// <summary>  
  240.     /// 获取当前目录下文件列表(仅文件)  
  241.     /// </summary>  
  242.     /// <returns></returns>  
  243.     public string[] GetFileList(string mask)  
  244.     {  
  245.         string[] downloadFiles;  
  246.         StringBuilder result = new StringBuilder();  
  247.         FtpWebRequest reqFTP;  
  248.         try  
  249.         {  
  250.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));  
  251.             reqFTP.UseBinary = true;  
  252.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  253.             reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;  
  254.             WebResponse response = reqFTP.GetResponse();  
  255.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  256.   
  257.             string line = reader.ReadLine();  
  258.             while (line != null)  
  259.             {  
  260.                 if (mask.Trim() != string.Empty && mask.Trim() != "*.*")  
  261.                 {  
  262.   
  263.                     string mask_ = mask.Substring(0, mask.IndexOf("*"));  
  264.                     if (line.Substring(0, mask_.Length) == mask_)  
  265.                     {  
  266.                         result.Append(line);  
  267.                         result.Append("\n");  
  268.                     }  
  269.                 }  
  270.                 else  
  271.                 {  
  272.                     result.Append(line);  
  273.                     result.Append("\n");  
  274.                 }  
  275.                 line = reader.ReadLine();  
  276.             }  
  277.             result.Remove(result.ToString().LastIndexOf('\n'), 1);  
  278.             reader.Close();  
  279.             response.Close();  
  280.             return result.ToString().Split('\n');  
  281.         }  
  282.         catch (Exception ex)  
  283.         {  
  284.             downloadFiles = null;  
  285.             throw ex;  
  286.         }  
  287.     }  
  288.   
  289.     /// <summary>  
  290.     /// 获取当前目录下所有的文件夹列表(仅文件夹)  
  291.     /// </summary>  
  292.     /// <returns></returns>  
  293.     public string[] GetDirectoryList()  
  294.     {  
  295.         string[] drectory = GetFilesDetailList();  
  296.         string m = string.Empty;  
  297.         foreach (string str in drectory)  
  298.         {  
  299.             int dirPos = str.IndexOf("<DIR>");  
  300.             if (dirPos > 0)  
  301.             {  
  302.                 /*判断 Windows 风格*/  
  303.                 m += str.Substring(dirPos + 5).Trim() + "\n";  
  304.             }  
  305.             else if (str.Trim().Substring(0, 1).ToUpper() == "D")  
  306.             {  
  307.                 /*判断 Unix 风格*/  
  308.                 string dir = str.Substring(54).Trim();  
  309.                 if (dir != "." && dir != "..")  
  310.                 {  
  311.                     m += dir + "\n";  
  312.                 }  
  313.             }  
  314.         }  
  315.   
  316.         char[] n = new char[] { '\n' };  
  317.         return m.Split(n);  
  318.     }  
  319.   
  320.     /// <summary>  
  321.     /// 判断当前目录下指定的子目录是否存在  
  322.     /// </summary>  
  323.     /// <param name="RemoteDirectoryName">指定的目录名</param>  
  324.     public bool DirectoryExist(string RemoteDirectoryName)  
  325.     {  
  326.         try  
  327.         {  
  328.             string[] dirList = GetDirectoryList();  
  329.   
  330.             foreach (string str in dirList)  
  331.             {  
  332.                 if (str.Trim() == RemoteDirectoryName.Trim())  
  333.                 {  
  334.                     return true;  
  335.                 }  
  336.             }  
  337.             return false;  
  338.         }  
  339.         catch  
  340.         {  
  341.             return false;  
  342.         }  
  343.   
  344.     }  
  345.   
  346.     /// <summary>  
  347.     /// 判断当前目录下指定的文件是否存在  
  348.     /// </summary>  
  349.     /// <param name="RemoteFileName">远程文件名</param>  
  350.     public bool FileExist(string RemoteFileName)  
  351.     {  
  352.         string[] fileList = GetFileList("*.*");  
  353.         foreach (string str in fileList)  
  354.         {  
  355.             if (str.Trim() == RemoteFileName.Trim())  
  356.             {  
  357.                 return true;  
  358.             }  
  359.         }  
  360.         return false;  
  361.     }  
  362.   
  363.     /// <summary>  
  364.     /// 创建文件夹  
  365.     /// </summary>  
  366.     /// <param name="dirName"></param>  
  367.     public void MakeDir(string dirName)  
  368.     {  
  369.         FtpWebRequest reqFTP;  
  370.         try  
  371.         {  
  372.             // dirName = name of the directory to create.  
  373.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));  
  374.             reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;  
  375.             reqFTP.UseBinary = true;  
  376.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  377.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  378.             Stream ftpStream = response.GetResponseStream();  
  379.   
  380.             ftpStream.Close();  
  381.             response.Close();  
  382.         }  
  383.         catch (Exception ex)  
  384.         {  
  385.             throw ex;  
  386.         }  
  387.     }  
  388.   
  389.     /// <summary>  
  390.     /// 获取指定文件大小  
  391.     /// </summary>  
  392.     /// <param name="filename"></param>  
  393.     /// <returns></returns>  
  394.     public long GetFileSize(string filename)  
  395.     {  
  396.         FtpWebRequest reqFTP;  
  397.         long fileSize = 0;  
  398.         try  
  399.         {  
  400.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));  
  401.             reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;  
  402.             reqFTP.UseBinary = true;  
  403.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  404.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  405.             Stream ftpStream = response.GetResponseStream();  
  406.             fileSize = response.ContentLength;  
  407.   
  408.             ftpStream.Close();  
  409.             response.Close();  
  410.         }  
  411.         catch (Exception ex)  
  412.         {  
  413.             throw ex;  
  414.         }  
  415.         return fileSize;  
  416.     }  
  417.   
  418.     /// <summary>  
  419.     /// 改名  
  420.     /// </summary>  
  421.     /// <param name="currentFilename"></param>  
  422.     /// <param name="newFilename"></param>  
  423.     public void ReName(string currentFilename, string newFilename)  
  424.     {  
  425.         FtpWebRequest reqFTP;  
  426.         try  
  427.         {  
  428.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));  
  429.             reqFTP.Method = WebRequestMethods.Ftp.Rename;  
  430.             reqFTP.RenameTo = newFilename;  
  431.             reqFTP.UseBinary = true;  
  432.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  433.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  434.             Stream ftpStream = response.GetResponseStream();  
  435.   
  436.             ftpStream.Close();  
  437.             response.Close();  
  438.         }  
  439.         catch (Exception ex)  
  440.         {  
  441.             throw ex;  
  442.         }  
  443.     }  
  444.   
  445.     /// <summary>  
  446.     /// 移动文件  
  447.     /// </summary>  
  448.     /// <param name="currentFilename"></param>  
  449.     /// <param name="newFilename"></param>  
  450.     public void MovieFile(string currentFilename, string newDirectory)  
  451.     {  
  452.         ReName(currentFilename, newDirectory);  
  453.     }  
  454.   
  455.     /// <summary>  
  456.     /// 切换当前目录  
  457.     /// </summary>  
  458.     /// <param name="DirectoryName"></param>  
  459.     /// <param name="IsRoot">true 绝对路径   false 相对路径</param>  
  460.     public void GotoDirectory(string DirectoryName, bool IsRoot)  
  461.     {  
  462.         if (IsRoot)  
  463.         {  
  464.             ftpRemotePath = DirectoryName;  
  465.         }  
  466.         else  
  467.         {  
  468.             ftpRemotePath += DirectoryName + "/";  
  469.         }  
  470.         ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";  
  471.     }  
  472. }  
  1. public class FtpWeb  
  2. {  
  3.     string ftpRemotePath;  
  4.     string ftpUserID;  
  5.     string ftpPassword;  
  6.     string ftpURI;  
  7.     string ftpServerIP;  
  8.   
  9.     /// <summary>  
  10.     /// 连接FTP  
  11.     /// </summary>  
  12.     /// <param name="FtpServerIP">FTP连接地址</param>  
  13.     /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>  
  14.     /// <param name="FtpUserID">用户名</param>  
  15.     /// <param name="FtpPassword">密码</param>  
  16.     public FtpWeb(string FtpServerIP, string FtpUserID, string FtpPassword)  
  17.     {  
  18.         ftpServerIP = FtpServerIP;  
  19.         ftpUserID = FtpUserID;  
  20.         ftpPassword = FtpPassword;  
  21.         ftpURI = FtpServerIP + "/";  
  22.     }  
  23.   
  24.     /// <summary>  
  25.     /// 上传  
  26.     /// </summary>  
  27.     /// <param name="filename"></param>  
  28.     public void Upload(string filename)  
  29.     {  
  30.         FileInfo fileInf = new FileInfo(filename);  
  31.         string uri = ftpURI + fileInf.Name;  
  32.         FtpWebRequest reqFTP;  
  33.   
  34.         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  35.         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  36.         reqFTP.KeepAlive = false;  
  37.         reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  38.         reqFTP.UseBinary = true;  
  39.         reqFTP.ContentLength = fileInf.Length;  
  40.         int buffLength = 2048;  
  41.         byte[] buff = new byte[buffLength];  
  42.         int contentLen;  
  43.         FileStream fs = fileInf.OpenRead();  
  44.         try  
  45.         {  
  46.             Stream strm = reqFTP.GetRequestStream();  
  47.             contentLen = fs.Read(buff, 0, buffLength);  
  48.             while (contentLen != 0)  
  49.             {  
  50.                 strm.Write(buff, 0, contentLen);  
  51.                 contentLen = fs.Read(buff, 0, buffLength);  
  52.             }  
  53.             strm.Close();  
  54.             fs.Close();  
  55.         }  
  56.         catch (Exception ex)  
  57.         {  
  58.             throw ex;  
  59.         }  
  60.     }  
  61.     /// <summary>  
  62.     /// 上传  
  63.     /// </summary>  
  64.     /// <param name="filename"></param>  
  65.     public void Upload2(string ftpuri, string filename)  
  66.     {  
  67.         FileInfo fileInf = new FileInfo(filename);  
  68.         string uri = ftpuri;  
  69.         FtpWebRequest reqFTP;  
  70.   
  71.         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  72.         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  73.         reqFTP.KeepAlive = false;  
  74.         reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  75.         reqFTP.UseBinary = true;  
  76.         reqFTP.ContentLength = fileInf.Length;  
  77.         int buffLength = 2048;  
  78.         byte[] buff = new byte[buffLength];  
  79.         int contentLen;  
  80.         FileStream fs = fileInf.OpenRead();  
  81.         try  
  82.         {  
  83.             Stream strm = reqFTP.GetRequestStream();  
  84.             contentLen = fs.Read(buff, 0, buffLength);  
  85.             while (contentLen != 0)  
  86.             {  
  87.                 strm.Write(buff, 0, contentLen);  
  88.                 contentLen = fs.Read(buff, 0, buffLength);  
  89.             }  
  90.             strm.Close();  
  91.             fs.Close();  
  92.         }  
  93.         catch (Exception ex)  
  94.         {  
  95.             throw ex;  
  96.         }  
  97.     }  
  98.     /// <summary>  
  99.     /// 下载  
  100.     /// </summary>  
  101.     /// <param name="filePath"></param>  
  102.     /// <param name="fileName"></param>  
  103.     public void Download(string filePath, string ftpUri, string fileName)  
  104.     {  
  105.         FtpWebRequest reqFTP;  
  106.         try  
  107.         {  
  108.             FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);  
  109.   
  110.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUri));  
  111.             reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
  112.             reqFTP.UseBinary = true;  
  113.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  114.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  115.             Stream ftpStream = response.GetResponseStream();  
  116.             long cl = response.ContentLength;  
  117.             int bufferSize = 2048;  
  118.             int readCount;  
  119.             byte[] buffer = new byte[bufferSize];  
  120.   
  121.             readCount = ftpStream.Read(buffer, 0, bufferSize);  
  122.             while (readCount > 0)  
  123.             {  
  124.                 outputStream.Write(buffer, 0, readCount);  
  125.                 readCount = ftpStream.Read(buffer, 0, bufferSize);  
  126.             }  
  127.   
  128.             ftpStream.Close();  
  129.             outputStream.Close();  
  130.             response.Close();  
  131.         }  
  132.         catch (Exception ex)  
  133.         {  
  134.             throw ex;  
  135.         }  
  136.     }  
  137.   
  138.   
  139.     /// <summary>  
  140.     /// 删除文件  
  141.     /// </summary>  
  142.     /// <param name="fileName"></param>  
  143.     public void Delete(string fileName)  
  144.     {  
  145.         try  
  146.         {  
  147.             string uri = fileName;  
  148.             FtpWebRequest reqFTP;  
  149.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  150.   
  151.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  152.             reqFTP.KeepAlive = false;  
  153.             reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;  
  154.   
  155.             string result = String.Empty;  
  156.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  157.             long size = response.ContentLength;  
  158.             Stream datastream = response.GetResponseStream();  
  159.             StreamReader sr = new StreamReader(datastream);  
  160.             result = sr.ReadToEnd();  
  161.             sr.Close();  
  162.             datastream.Close();  
  163.             response.Close();  
  164.         }  
  165.         catch (Exception ex)  
  166.         {  
  167.             throw ex;  
  168.         }  
  169.     }  
  170.   
  171.     /// <summary>  
  172.     /// 删除文件夹  
  173.     /// </summary>  
  174.     /// <param name="folderName"></param>  
  175.     public void RemoveDirectory(string folderName)  
  176.     {  
  177.         try  
  178.         {  
  179.             string uri = ftpURI + folderName;  
  180.             FtpWebRequest reqFTP;  
  181.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
  182.   
  183.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  184.             reqFTP.KeepAlive = false;  
  185.             reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;  
  186.   
  187.             string result = String.Empty;  
  188.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  189.             long size = response.ContentLength;  
  190.             Stream datastream = response.GetResponseStream();  
  191.             StreamReader sr = new StreamReader(datastream);  
  192.             result = sr.ReadToEnd();  
  193.             sr.Close();  
  194.             datastream.Close();  
  195.             response.Close();  
  196.         }  
  197.         catch (Exception ex)  
  198.         {  
  199.             throw ex;  
  200.         }  
  201.     }  
  202.   
  203.     /// <summary>  
  204.     /// 获取当前目录下明细(包含文件和文件夹)  
  205.     /// </summary>  
  206.     /// <returns></returns>  
  207.     public string[] GetFilesDetailList()  
  208.     {  
  209.         string[] downloadFiles;  
  210.         try  
  211.         {  
  212.             StringBuilder result = new StringBuilder();  
  213.             FtpWebRequest ftp;  
  214.             ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));  
  215.             ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  216.             ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;  
  217.             WebResponse response = ftp.GetResponse();  
  218.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  219.             string line = reader.ReadLine();  
  220.   
  221.             while (line != null)  
  222.             {  
  223.                 result.Append(line);  
  224.                 result.Append("\n");  
  225.                 line = reader.ReadLine();  
  226.             }  
  227.             result.Remove(result.ToString().LastIndexOf("\n"), 1);  
  228.             reader.Close();  
  229.             response.Close();  
  230.             return result.ToString().Split('\n');  
  231.         }  
  232.         catch (Exception ex)  
  233.         {  
  234.             downloadFiles = null;  
  235.             throw ex;  
  236.         }  
  237.     }  
  238.   
  239.     /// <summary>  
  240.     /// 获取当前目录下文件列表(仅文件)  
  241.     /// </summary>  
  242.     /// <returns></returns>  
  243.     public string[] GetFileList(string mask)  
  244.     {  
  245.         string[] downloadFiles;  
  246.         StringBuilder result = new StringBuilder();  
  247.         FtpWebRequest reqFTP;  
  248.         try  
  249.         {  
  250.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));  
  251.             reqFTP.UseBinary = true;  
  252.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  253.             reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;  
  254.             WebResponse response = reqFTP.GetResponse();  
  255.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  256.   
  257.             string line = reader.ReadLine();  
  258.             while (line != null)  
  259.             {  
  260.                 if (mask.Trim() != string.Empty && mask.Trim() != "*.*")  
  261.                 {  
  262.   
  263.                     string mask_ = mask.Substring(0, mask.IndexOf("*"));  
  264.                     if (line.Substring(0, mask_.Length) == mask_)  
  265.                     {  
  266.                         result.Append(line);  
  267.                         result.Append("\n");  
  268.                     }  
  269.                 }  
  270.                 else  
  271.                 {  
  272.                     result.Append(line);  
  273.                     result.Append("\n");  
  274.                 }  
  275.                 line = reader.ReadLine();  
  276.             }  
  277.             result.Remove(result.ToString().LastIndexOf('\n'), 1);  
  278.             reader.Close();  
  279.             response.Close();  
  280.             return result.ToString().Split('\n');  
  281.         }  
  282.         catch (Exception ex)  
  283.         {  
  284.             downloadFiles = null;  
  285.             throw ex;  
  286.         }  
  287.     }  
  288.   
  289.     /// <summary>  
  290.     /// 获取当前目录下所有的文件夹列表(仅文件夹)  
  291.     /// </summary>  
  292.     /// <returns></returns>  
  293.     public string[] GetDirectoryList()  
  294.     {  
  295.         string[] drectory = GetFilesDetailList();  
  296.         string m = string.Empty;  
  297.         foreach (string str in drectory)  
  298.         {  
  299.             int dirPos = str.IndexOf("<DIR>");  
  300.             if (dirPos > 0)  
  301.             {  
  302.                 /*判断 Windows 风格*/  
  303.                 m += str.Substring(dirPos + 5).Trim() + "\n";  
  304.             }  
  305.             else if (str.Trim().Substring(0, 1).ToUpper() == "D")  
  306.             {  
  307.                 /*判断 Unix 风格*/  
  308.                 string dir = str.Substring(54).Trim();  
  309.                 if (dir != "." && dir != "..")  
  310.                 {  
  311.                     m += dir + "\n";  
  312.                 }  
  313.             }  
  314.         }  
  315.   
  316.         char[] n = new char[] { '\n' };  
  317.         return m.Split(n);  
  318.     }  
  319.   
  320.     /// <summary>  
  321.     /// 判断当前目录下指定的子目录是否存在  
  322.     /// </summary>  
  323.     /// <param name="RemoteDirectoryName">指定的目录名</param>  
  324.     public bool DirectoryExist(string RemoteDirectoryName)  
  325.     {  
  326.         try  
  327.         {  
  328.             string[] dirList = GetDirectoryList();  
  329.   
  330.             foreach (string str in dirList)  
  331.             {  
  332.                 if (str.Trim() == RemoteDirectoryName.Trim())  
  333.                 {  
  334.                     return true;  
  335.                 }  
  336.             }  
  337.             return false;  
  338.         }  
  339.         catch  
  340.         {  
  341.             return false;  
  342.         }  
  343.   
  344.     }  
  345.   
  346.     /// <summary>  
  347.     /// 判断当前目录下指定的文件是否存在  
  348.     /// </summary>  
  349.     /// <param name="RemoteFileName">远程文件名</param>  
  350.     public bool FileExist(string RemoteFileName)  
  351.     {  
  352.         string[] fileList = GetFileList("*.*");  
  353.         foreach (string str in fileList)  
  354.         {  
  355.             if (str.Trim() == RemoteFileName.Trim())  
  356.             {  
  357.                 return true;  
  358.             }  
  359.         }  
  360.         return false;  
  361.     }  
  362.   
  363.     /// <summary>  
  364.     /// 创建文件夹  
  365.     /// </summary>  
  366.     /// <param name="dirName"></param>  
  367.     public void MakeDir(string dirName)  
  368.     {  
  369.         FtpWebRequest reqFTP;  
  370.         try  
  371.         {  
  372.             // dirName = name of the directory to create.  
  373.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));  
  374.             reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;  
  375.             reqFTP.UseBinary = true;  
  376.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  377.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  378.             Stream ftpStream = response.GetResponseStream();  
  379.   
  380.             ftpStream.Close();  
  381.             response.Close();  
  382.         }  
  383.         catch (Exception ex)  
  384.         {  
  385.             throw ex;  
  386.         }  
  387.     }  
  388.   
  389.     /// <summary>  
  390.     /// 获取指定文件大小  
  391.     /// </summary>  
  392.     /// <param name="filename"></param>  
  393.     /// <returns></returns>  
  394.     public long GetFileSize(string filename)  
  395.     {  
  396.         FtpWebRequest reqFTP;  
  397.         long fileSize = 0;  
  398.         try  
  399.         {  
  400.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));  
  401.             reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;  
  402.             reqFTP.UseBinary = true;  
  403.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  404.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  405.             Stream ftpStream = response.GetResponseStream();  
  406.             fileSize = response.ContentLength;  
  407.   
  408.             ftpStream.Close();  
  409.             response.Close();  
  410.         }  
  411.         catch (Exception ex)  
  412.         {  
  413.             throw ex;  
  414.         }  
  415.         return fileSize;  
  416.     }  
  417.   
  418.     /// <summary>  
  419.     /// 改名  
  420.     /// </summary>  
  421.     /// <param name="currentFilename"></param>  
  422.     /// <param name="newFilename"></param>  
  423.     public void ReName(string currentFilename, string newFilename)  
  424.     {  
  425.         FtpWebRequest reqFTP;  
  426.         try  
  427.         {  
  428.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));  
  429.             reqFTP.Method = WebRequestMethods.Ftp.Rename;  
  430.             reqFTP.RenameTo = newFilename;  
  431.             reqFTP.UseBinary = true;  
  432.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  433.             FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  434.             Stream ftpStream = response.GetResponseStream();  
  435.   
  436.             ftpStream.Close();  
  437.             response.Close();  
  438.         }  
  439.         catch (Exception ex)  
  440.         {  
  441.             throw ex;  
  442.         }  
  443.     }  
  444.   
  445.     /// <summary>  
  446.     /// 移动文件  
  447.     /// </summary>  
  448.     /// <param name="currentFilename"></param>  
  449.     /// <param name="newFilename"></param>  
  450.     public void MovieFile(string currentFilename, string newDirectory)  
  451.     {  
  452.         ReName(currentFilename, newDirectory);  
  453.     }  
  454.   
  455.     /// <summary>  
  456.     /// 切换当前目录  
  457.     /// </summary>  
  458.     /// <param name="DirectoryName"></param>  
  459.     /// <param name="IsRoot">true 绝对路径   false 相对路径</param>  
  460.     public void GotoDirectory(string DirectoryName, bool IsRoot)  
  461.     {  
  462.         if (IsRoot)  
  463.         {  
  464.             ftpRemotePath = DirectoryName;  
  465.         }  
  466.         else  
  467.         {  
  468.             ftpRemotePath += DirectoryName + "/";  
  469.         }  
  470.         ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";  
  471.     }  
  472. }  
0 0
原创粉丝点击