FTP上传类FTP上传类

[c-sharp] view plain copy print ?
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.Net; 
  5. using System.IO; 
  6. using System.Globalization; 
  7. using System.Text.RegularExpressions; 
  8. namespace WebBaseLib 
  9.     /// <summary> 
  10.     /// FTP处理操作类 
  11.     /// 功能: 
  12.     /// 下载文件 
  13.     /// 上传文件 
  14.     /// 上传文件的进度信息 
  15.     /// 下载文件的进度信息 
  16.     /// 删除文件 
  17.     /// 列出文件 
  18.     /// 列出目录 
  19.     /// 进入子目录 
  20.     /// 退出当前目录返回上一层目录 
  21.     /// 判断远程文件是否存在 
  22.     /// 判断远程文件是否存在 
  23.     /// 删除远程文件    
  24.     /// 建立目录 
  25.     /// 删除目录 
  26.     /// 文件(目录)改名 
  27.   
  28.     /// </summary> 
  29.     /// <remarks> 
  30.     /// 创建人:南疯 
  31.     /// 创建时间:2007年4月28日 
  32.     /// </remarks> 
  33.     #region 文件信息结构 
  34.     public struct FileStruct 
  35.     { 
  36.         public string Flags; 
  37.         public string Owner; 
  38.         public string Group; 
  39.         public bool IsDirectory; 
  40.         public DateTime CreateTime; 
  41.         public string Name; 
  42.     } 
  43.     public enum FileListStyle 
  44.     { 
  45.         UnixStyle, 
  46.         WindowsStyle, 
  47.         Unknown 
  48.     } 
  49.     #endregion 
  50.     public class NewFtp 
  51.     { 
  52.         #region 属性信息 
  53.         /// <summary> 
  54.         /// FTP请求对象 
  55.         /// </summary> 
  56.         FtpWebRequest Request = null
  57.         /// <summary> 
  58.         /// FTP响应对象 
  59.         /// </summary> 
  60.         FtpWebResponse Response = null
  61.         /// <summary> 
  62.         /// FTP服务器地址 
  63.         /// </summary> 
  64.         private Uri _Uri; 
  65.         /// <summary> 
  66.         /// FTP服务器地址 
  67.         /// </summary> 
  68.         public Uri Uri 
  69.         { 
  70.             get 
  71.             { 
  72.                 if( _DirectoryPath == "/"
  73.                 { 
  74.                     return _Uri; 
  75.                 } 
  76.                 else 
  77.                 { 
  78.                     string strUri = _Uri.ToString(); 
  79.                     if( strUri.EndsWith( "/" ) ) 
  80.                     { 
  81.                         strUri = strUri.Substring( 0, strUri.Length - 1 ); 
  82.                     } 
  83.                     return new Uri( strUri + this.DirectoryPath ); 
  84.                 } 
  85.             } 
  86.             set 
  87.             { 
  88.                 if( value.Scheme != Uri.UriSchemeFtp ) 
  89.                 { 
  90.                     throw new Exception( "Ftp 地址格式错误!" ); 
  91.                 } 
  92.                 _Uri = new Uri( value.GetLeftPart( UriPartial.Authority ) ); 
  93.                 _DirectoryPath = value.AbsolutePath; 
  94.                 if( !_DirectoryPath.EndsWith( "/" ) ) 
  95.                 { 
  96.                     _DirectoryPath += "/"
  97.                 } 
  98.             } 
  99.         } 
  100.   
  101.         /// <summary> 
  102.         /// 当前工作目录 
  103.         /// </summary> 
  104.         private string _DirectoryPath; 
  105.   
  106.         /// <summary> 
  107.         /// 当前工作目录 
  108.         /// </summary> 
  109.         public string DirectoryPath 
  110.         { 
  111.             get 
  112.             { 
  113.                 return _DirectoryPath; 
  114.             } 
  115.             set 
  116.             { 
  117.                 _DirectoryPath = value; 
  118.             } 
  119.         } 
  120.   
  121.         /// <summary> 
  122.         /// FTP登录用户 
  123.         /// </summary> 
  124.         private string _UserName; 
  125.         /// <summary> 
  126.         /// FTP登录用户 
  127.         /// </summary> 
  128.         public string UserName 
  129.         { 
  130.             get 
  131.             { 
  132.                 return _UserName; 
  133.             } 
  134.             set 
  135.             { 
  136.                 _UserName = value; 
  137.             } 
  138.         } 
  139.   
  140.         /// <summary> 
  141.         /// 错误信息 
  142.         /// </summary> 
  143.         private string _ErrorMsg; 
  144.         /// <summary> 
  145.         /// 错误信息 
  146.         /// </summary> 
  147.         public string ErrorMsg 
  148.         { 
  149.             get 
  150.             { 
  151.                 return _ErrorMsg; 
  152.             } 
  153.             set 
  154.             { 
  155.                 _ErrorMsg = value; 
  156.             } 
  157.         } 
  158.   
  159.         /// <summary> 
  160.         /// FTP登录密码 
  161.         /// </summary> 
  162.         private string _Password; 
  163.         /// <summary> 
  164.         /// FTP登录密码 
  165.         /// </summary> 
  166.         public string Password 
  167.         { 
  168.             get 
  169.             { 
  170.                 return _Password; 
  171.             } 
  172.             set 
  173.             { 
  174.                 _Password = value; 
  175.             } 
  176.         } 
  177.   
  178.         /// <summary> 
  179.         /// 连接FTP服务器的代理服务 
  180.         /// </summary> 
  181.         private WebProxy _Proxy = null
  182.         /// <summary> 
  183.         /// 连接FTP服务器的代理服务 
  184.         /// </summary> 
  185.         public WebProxy Proxy 
  186.         { 
  187.             get 
  188.             { 
  189.                 return _Proxy; 
  190.             } 
  191.             set 
  192.             { 
  193.                 _Proxy = value; 
  194.             } 
  195.         } 
  196.   
  197.         /// <summary> 
  198.         /// 是否需要删除临时文件 
  199.         /// </summary> 
  200.         private bool _isDeleteTempFile = false
  201.         /// <summary> 
  202.         /// 异步上传所临时生成的文件 
  203.         /// </summary> 
  204.         private string _UploadTempFile = ""
  205.         #endregion 
  206.         #region 事件 
  207.         public delegate void De_DownloadProgressChanged( object sender, DownloadProgressChangedEventArgs e ); 
  208.         public delegate void De_DownloadDataCompleted( object sender, System.ComponentModel.AsyncCompletedEventArgs e ); 
  209.         public delegate void De_UploadProgressChanged( object sender, UploadProgressChangedEventArgs e ); 
  210.         public delegate void De_UploadFileCompleted( object sender, UploadFileCompletedEventArgs e ); 
  211.   
  212.         /// <summary> 
  213.         /// 异步下载进度发生改变触发的事件 
  214.         /// </summary> 
  215.         public event De_DownloadProgressChanged DownloadProgressChanged; 
  216.         /// <summary> 
  217.         /// 异步下载文件完成之后触发的事件 
  218.         /// </summary> 
  219.         public event De_DownloadDataCompleted DownloadDataCompleted; 
  220.         /// <summary> 
  221.         /// 异步上传进度发生改变触发的事件 
  222.         /// </summary> 
  223.         public event De_UploadProgressChanged UploadProgressChanged; 
  224.         /// <summary> 
  225.         /// 异步上传文件完成之后触发的事件 
  226.         /// </summary> 
  227.         public event De_UploadFileCompleted UploadFileCompleted; 
  228.         #endregion 
  229.         #region 构造析构函数 
  230.         /// <summary> 
  231.         /// 构造函数 
  232.         /// </summary> 
  233.         /// <param name="FtpUri">FTP地址</param> 
  234.         /// <param name="strUserName">登录用户名</param> 
  235.         /// <param name="strPassword">登录密码</param> 
  236.         public NewFtp( Uri FtpUri, string strUserName, string strPassword ) 
  237.         { 
  238.             this._Uri = new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) ); 
  239.             _DirectoryPath = FtpUri.AbsolutePath; 
  240.             if( !_DirectoryPath.EndsWith( "/" ) ) 
  241.             { 
  242.                 _DirectoryPath += "/"
  243.             } 
  244.             this._UserName = strUserName; 
  245.             this._Password = strPassword; 
  246.             this._Proxy = null
  247.         } 
  248.         /// <summary> 
  249.         /// 构造函数 
  250.         /// </summary> 
  251.         /// <param name="FtpUri">FTP地址</param> 
  252.         /// <param name="strUserName">登录用户名</param> 
  253.         /// <param name="strPassword">登录密码</param> 
  254.         /// <param name="objProxy">连接代理</param> 
  255.         public NewFtp( Uri FtpUri, string strUserName, string strPassword, WebProxy objProxy ) 
  256.         { 
  257.             this._Uri = new Uri( FtpUri.GetLeftPart( UriPartial.Authority ) ); 
  258.             _DirectoryPath = FtpUri.AbsolutePath; 
  259.             if( !_DirectoryPath.EndsWith( "/" ) ) 
  260.             { 
  261.                 _DirectoryPath += "/"
  262.             } 
  263.             this._UserName = strUserName; 
  264.             this._Password = strPassword; 
  265.             this._Proxy = objProxy; 
  266.         } 
  267.         /// <summary> 
  268.         /// 构造函数 
  269.         /// </summary> 
  270.         public NewFtp() 
  271.         { 
  272.             this._UserName = "anonymous"//匿名用户 
  273.             this._Password = "@anonymous"
  274.             this._Uri = null
  275.             this._Proxy = null
  276.         } 
  277.   
  278.         /// <summary> 
  279.         /// 析构函数 
  280.         /// </summary> 
  281.         ~NewFtp() 
  282.         { 
  283.             if( Response != null
  284.             { 
  285.                 Response.Close(); 
  286.                 Response = null
  287.             } 
  288.             if( Request != null
  289.             { 
  290.                 Request.Abort(); 
  291.                 Request = null
  292.             } 
  293.         } 
  294.         #endregion 
  295.         #region 建立连接 
  296.         /// <summary> 
  297.         /// 建立FTP链接,返回响应对象 
  298.         /// </summary> 
  299.         /// <param name="uri">FTP地址</param> 
  300.         /// <param name="FtpMathod">操作命令</param> 
  301.         private FtpWebResponse Open( Uri uri, string FtpMathod ) 
  302.         { 
  303.             try 
  304.             { 
  305.                 Request = ( FtpWebRequest ) WebRequest.Create( uri ); 
  306.                 Request.Method = FtpMathod; 
  307.                 Request.UseBinary = true
  308.                 Request.Credentials = new NetworkCredential( this.UserName, this.Password ); 
  309.                 if( this.Proxy != null
  310.                 { 
  311.                     Request.Proxy = this.Proxy; 
  312.                 } 
  313.                 return ( FtpWebResponse ) Request.GetResponse(); 
  314.             } 
  315.             catch( Exception ep ) 
  316.             { 
  317.                 ErrorMsg = ep.ToString(); 
  318.                 throw ep; 
  319.             } 
  320.         } 
  321.         /// <summary> 
  322.         /// 建立FTP链接,返回请求对象 
  323.         /// </summary> 
  324.         /// <param name="uri">FTP地址</param> 
  325.         /// <param name="FtpMathod">操作命令</param> 
  326.         private FtpWebRequest OpenRequest( Uri uri, string FtpMathod ) 
  327.         { 
  328.             try 
  329.             { 
  330.                 Request = ( FtpWebRequest ) WebRequest.Create( uri ); 
  331.                 Request.Method = FtpMathod; 
  332.                 Request.UseBinary = true
  333.                 Request.Credentials = new NetworkCredential( this.UserName, this.Password ); 
  334.                 if( this.Proxy != null
  335.                 { 
  336.                     Request.Proxy = this.Proxy; 
  337.                 } 
  338.                 return Request; 
  339.             } 
  340.             catch( Exception ep ) 
  341.             { 
  342.                 ErrorMsg = ep.ToString(); 
  343.                 throw ep; 
  344.             } 
  345.         } 
  346.         #endregion 
  347.         #region 下载文件 
  348.   
  349.         /// <summary> 
  350.         /// 从FTP服务器下载文件,使用与远程文件同名的文件名来保存文件 
  351.         /// </summary> 
  352.         /// <param name="RemoteFileName">远程文件名</param> 
  353.         /// <param name="LocalPath">本地路径</param> 
  354.   
  355.         public bool DownloadFile( string RemoteFileName, string LocalPath ) 
  356.         { 
  357.             return DownloadFile( RemoteFileName, LocalPath, RemoteFileName ); 
  358.         } 
  359.         /// <summary> 
  360.         /// 从FTP服务器下载文件,指定本地路径和本地文件名 
  361.         /// </summary> 
  362.         /// <param name="RemoteFileName">远程文件名</param> 
  363.         /// <param name="LocalPath">本地路径</param> 
  364.         /// <param name="LocalFilePath">保存文件的本地路径,后面带有""</param> 
  365.         /// <param name="LocalFileName">保存本地的文件名</param> 
  366.         public bool DownloadFile( string RemoteFileName, string LocalPath, string LocalFileName ) 
  367.         { 
  368.             byte[] bt = null
  369.             try 
  370.             { 
  371.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) ) 
  372.                 { 
  373.                     throw new Exception( "非法文件名或目录名!" ); 
  374.                 } 
  375.                 if( !Directory.Exists( LocalPath ) ) 
  376.                 { 
  377.                     throw new Exception( "本地文件路径不存在!" ); 
  378.                 } 
  379.   
  380.                 string LocalFullPath = Path.Combine( LocalPath, LocalFileName ); 
  381.                 if( File.Exists( LocalFullPath ) ) 
  382.                 { 
  383.                     throw new Exception( "当前路径下已经存在同名文件!" ); 
  384.                 } 
  385.                 bt = DownloadFile( RemoteFileName ); 
  386.                 if( bt != null
  387.                 { 
  388.                     FileStream stream = new FileStream( LocalFullPath, FileMode.Create ); 
  389.                     stream.Write( bt, 0, bt.Length ); 
  390.                     stream.Flush(); 
  391.                     stream.Close(); 
  392.                     return true
  393.                 } 
  394.                 else 
  395.                 { 
  396.                     return false
  397.                 } 
  398.             } 
  399.             catch( Exception ep ) 
  400.             { 
  401.                 ErrorMsg = ep.ToString(); 
  402.                 throw ep; 
  403.             } 
  404.         } 
  405.   
  406.         /// <summary> 
  407.         /// 从FTP服务器下载文件,返回文件二进制数据 
  408.         /// </summary> 
  409.         /// <param name="RemoteFileName">远程文件名</param> 
  410.         public byte[] DownloadFile( string RemoteFileName ) 
  411.         { 
  412.             try 
  413.             { 
  414.                 if( !IsValidFileChars( RemoteFileName ) ) 
  415.                 { 
  416.                     throw new Exception( "非法文件名或目录名!" ); 
  417.                 } 
  418.                 Response = Open( new Uri( this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.DownloadFile ); 
  419.                 Stream Reader = Response.GetResponseStream(); 
  420.   
  421.                 MemoryStream mem = new MemoryStream( 1024 * 500 ); 
  422.                 byte[] buffer = new byte[ 1024 ]; 
  423.                 int bytesRead = 0; 
  424.                 int TotalByteRead = 0; 
  425.                 while( true
  426.                 { 
  427.                     bytesRead = Reader.Read( buffer, 0, buffer.Length ); 
  428.                     TotalByteRead += bytesRead; 
  429.                     if( bytesRead == 0 ) 
  430.                         break
  431.                     mem.Write( buffer, 0, bytesRead ); 
  432.                 } 
  433.                 if( mem.Length > 0 ) 
  434.                 { 
  435.                     return mem.ToArray(); 
  436.                 } 
  437.                 else 
  438.                 { 
  439.                     return null
  440.                 } 
  441.             } 
  442.             catch( Exception ep ) 
  443.             { 
  444.                 ErrorMsg = ep.ToString(); 
  445.                 throw ep; 
  446.             } 
  447.         } 
  448.         #endregion 
  449.         #region 异步下载文件 
  450.         /// <summary> 
  451.         /// 从FTP服务器异步下载文件,指定本地路径和本地文件名 
  452.         /// </summary> 
  453.         /// <param name="RemoteFileName">远程文件名</param>        
  454.         /// <param name="LocalPath">保存文件的本地路径,后面带有""</param> 
  455.         /// <param name="LocalFileName">保存本地的文件名</param> 
  456.         public void DownloadFileAsync( string RemoteFileName, string LocalPath, string LocalFileName ) 
  457.         { 
  458.             byte[] bt = null
  459.             try 
  460.             { 
  461.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( LocalFileName ) || !IsValidPathChars( LocalPath ) ) 
  462.                 { 
  463.                     throw new Exception( "非法文件名或目录名!" ); 
  464.                 } 
  465.                 if( !Directory.Exists( LocalPath ) ) 
  466.                 { 
  467.                     throw new Exception( "本地文件路径不存在!" ); 
  468.                 } 
  469.   
  470.                 string LocalFullPath = Path.Combine( LocalPath, LocalFileName ); 
  471.                 if( File.Exists( LocalFullPath ) ) 
  472.                 { 
  473.                     throw new Exception( "当前路径下已经存在同名文件!" ); 
  474.                 } 
  475.                 DownloadFileAsync( RemoteFileName, LocalFullPath ); 
  476.   
  477.             } 
  478.             catch( Exception ep ) 
  479.             { 
  480.                 ErrorMsg = ep.ToString(); 
  481.                 throw ep; 
  482.             } 
  483.         } 
  484.   
  485.         /// <summary> 
  486.         /// 从FTP服务器异步下载文件,指定本地完整路径文件名 
  487.         /// </summary> 
  488.         /// <param name="RemoteFileName">远程文件名</param> 
  489.         /// <param name="LocalFullPath">本地完整路径文件名</param> 
  490.         public void DownloadFileAsync( string RemoteFileName, string LocalFullPath ) 
  491.         { 
  492.             try 
  493.             { 
  494.                 if( !IsValidFileChars( RemoteFileName ) ) 
  495.                 { 
  496.                     throw new Exception( "非法文件名或目录名!" ); 
  497.                 } 
  498.                 if( File.Exists( LocalFullPath ) ) 
  499.                 { 
  500.                     throw new Exception( "当前路径下已经存在同名文件!" ); 
  501.                 } 
  502.                 MyWebClient client = new MyWebClient(); 
  503.   
  504.                 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler( client_DownloadProgressChanged ); 
  505.                 client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler( client_DownloadFileCompleted ); 
  506.                 client.Credentials = new NetworkCredential( this.UserName, this.Password ); 
  507.                 if( this.Proxy != null
  508.                 { 
  509.                     client.Proxy = this.Proxy; 
  510.                 } 
  511.                 client.DownloadFileAsync( new Uri( this.Uri.ToString() + RemoteFileName ), LocalFullPath ); 
  512.             } 
  513.             catch( Exception ep ) 
  514.             { 
  515.                 ErrorMsg = ep.ToString(); 
  516.                 throw ep; 
  517.             } 
  518.         } 
  519.   
  520.         /// <summary> 
  521.         /// 异步下载文件完成之后触发的事件 
  522.         /// </summary> 
  523.         /// <param name="sender">下载对象</param> 
  524.         /// <param name="e">数据信息对象</param> 
  525.         void client_DownloadFileCompleted( object sender, System.ComponentModel.AsyncCompletedEventArgs e ) 
  526.         { 
  527.             if( DownloadDataCompleted != null
  528.             { 
  529.                 DownloadDataCompleted( sender, e ); 
  530.             } 
  531.         } 
  532.   
  533.         /// <summary> 
  534.         /// 异步下载进度发生改变触发的事件 
  535.         /// </summary> 
  536.         /// <param name="sender">下载对象</param> 
  537.         /// <param name="e">进度信息对象</param> 
  538.         void client_DownloadProgressChanged( object sender, DownloadProgressChangedEventArgs e ) 
  539.         { 
  540.             if( DownloadProgressChanged != null
  541.             { 
  542.                 DownloadProgressChanged( sender, e ); 
  543.             } 
  544.         } 
  545.         #endregion 
  546.         #region 上传文件 
  547.         /// <summary> 
  548.         /// 上传文件到FTP服务器 
  549.         /// </summary> 
  550.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  551.         public bool UploadFile( string LocalFullPath ) 
  552.         { 
  553.             return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ), false ); 
  554.         } 
  555.         /// <summary> 
  556.         /// 上传文件到FTP服务器 
  557.         /// </summary> 
  558.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  559.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  560.         public bool UploadFile( string LocalFullPath, bool OverWriteRemoteFile ) 
  561.         { 
  562.             return UploadFile( LocalFullPath, Path.GetFileName( LocalFullPath ), OverWriteRemoteFile ); 
  563.         } 
  564.         /// <summary> 
  565.         /// 上传文件到FTP服务器 
  566.         /// </summary> 
  567.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  568.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  569.         public bool UploadFile( string LocalFullPath, string RemoteFileName ) 
  570.         { 
  571.             return UploadFile( LocalFullPath, RemoteFileName, false ); 
  572.         } 
  573.         /// <summary> 
  574.         /// 上传文件到FTP服务器 
  575.         /// </summary> 
  576.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  577.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  578.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  579.         public bool UploadFile( string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile ) 
  580.         { 
  581.             try 
  582.             { 
  583.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( Path.GetFileName( LocalFullPath ) ) || !IsValidPathChars( Path.GetDirectoryName( LocalFullPath ) ) ) 
  584.                 { 
  585.                     throw new Exception( "非法文件名或目录名!" ); 
  586.                 } 
  587.                 if( File.Exists( LocalFullPath ) ) 
  588.                 { 
  589.                     FileStream Stream = new FileStream( LocalFullPath, FileMode.Open, FileAccess.Read ); 
  590.                     byte[] bt = new byte[ Stream.Length ]; 
  591.                     Stream.Read( bt, 0, ( Int32 ) Stream.Length );   //注意,因为Int32的最大限制,最大上传文件只能是大约2G多一点 
  592.                     Stream.Close(); 
  593.                     return UploadFile( bt, RemoteFileName, OverWriteRemoteFile ); 
  594.                 } 
  595.                 else 
  596.                 { 
  597.                     throw new Exception( "本地文件不存在!" ); 
  598.                 } 
  599.             } 
  600.             catch( Exception ep ) 
  601.             { 
  602.                 ErrorMsg = ep.ToString(); 
  603.                 throw ep; 
  604.             } 
  605.         } 
  606.         /// <summary> 
  607.         /// 上传文件到FTP服务器 
  608.         /// </summary> 
  609.         /// <param name="FileBytes">上传的二进制数据</param> 
  610.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  611.         public bool UploadFile( byte[] FileBytes, string RemoteFileName ) 
  612.         { 
  613.             if( !IsValidFileChars( RemoteFileName ) ) 
  614.             { 
  615.                 throw new Exception( "非法文件名或目录名!" ); 
  616.             } 
  617.             return UploadFile( FileBytes, RemoteFileName, false ); 
  618.         } 
  619.         /// <summary> 
  620.         /// 上传文件到FTP服务器 
  621.         /// </summary> 
  622.         /// <param name="FileBytes">文件二进制内容</param> 
  623.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  624.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  625.         public bool UploadFile( byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile ) 
  626.         { 
  627.             try 
  628.             { 
  629.                 if( !IsValidFileChars( RemoteFileName ) ) 
  630.                 { 
  631.                     throw new Exception( "非法文件名!" ); 
  632.                 } 
  633.                 if( !OverWriteRemoteFile && FileExist( RemoteFileName ) ) 
  634.                 { 
  635.                     throw new Exception( "FTP服务上面已经存在同名文件!" ); 
  636.                 } 
  637.                 Response = Open( new Uri( this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.UploadFile ); 
  638.                 Stream requestStream = Request.GetRequestStream(); 
  639.                 MemoryStream mem = new MemoryStream( FileBytes ); 
  640.   
  641.                 byte[] buffer = new byte[ 1024 ]; 
  642.                 int bytesRead = 0; 
  643.                 int TotalRead = 0; 
  644.                 while( true
  645.                 { 
  646.                     bytesRead = mem.Read( buffer, 0, buffer.Length ); 
  647.                     if( bytesRead == 0 ) 
  648.                         break
  649.                     TotalRead += bytesRead; 
  650.                     requestStream.Write( buffer, 0, bytesRead ); 
  651.                 } 
  652.                 requestStream.Close(); 
  653.                 Response = ( FtpWebResponse ) Request.GetResponse(); 
  654.                 mem.Close(); 
  655.                 mem.Dispose(); 
  656.                 FileBytes = null
  657.                 return true
  658.             } 
  659.             catch( Exception ep ) 
  660.             { 
  661.                 ErrorMsg = ep.ToString(); 
  662.                 throw ep; 
  663.             } 
  664.         } 
  665.         #endregion 
  666.         #region 异步上传文件 
  667.         /// <summary> 
  668.         /// 异步上传文件到FTP服务器 
  669.         /// </summary> 
  670.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  671.         public void UploadFileAsync( string LocalFullPath ) 
  672.         { 
  673.             UploadFileAsync( LocalFullPath, Path.GetFileName( LocalFullPath ), false ); 
  674.         } 
  675.         /// <summary> 
  676.         /// 异步上传文件到FTP服务器 
  677.         /// </summary> 
  678.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  679.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  680.         public void UploadFileAsync( string LocalFullPath, bool OverWriteRemoteFile ) 
  681.         { 
  682.             UploadFileAsync( LocalFullPath, Path.GetFileName( LocalFullPath ), OverWriteRemoteFile ); 
  683.         } 
  684.         /// <summary> 
  685.         /// 异步上传文件到FTP服务器 
  686.         /// </summary> 
  687.         /// <param name="LocalFullPath">本地带有完整路径的文件</param> 
  688.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  689.         public void UploadFileAsync( string LocalFullPath, string RemoteFileName ) 
  690.         { 
  691.             UploadFileAsync( LocalFullPath, RemoteFileName, false ); 
  692.         } 
  693.         /// <summary> 
  694.         /// 异步上传文件到FTP服务器 
  695.         /// </summary> 
  696.         /// <param name="LocalFullPath">本地带有完整路径的文件名</param> 
  697.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  698.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  699.         public void UploadFileAsync( string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile ) 
  700.         { 
  701.             try 
  702.             { 
  703.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( Path.GetFileName( LocalFullPath ) ) || !IsValidPathChars( Path.GetDirectoryName( LocalFullPath ) ) ) 
  704.                 { 
  705.                     throw new Exception( "非法文件名或目录名!" ); 
  706.                 } 
  707.                 if( !OverWriteRemoteFile && FileExist( RemoteFileName ) ) 
  708.                 { 
  709.                     throw new Exception( "FTP服务上面已经存在同名文件!" ); 
  710.                 } 
  711.                 if( File.Exists( LocalFullPath ) ) 
  712.                 { 
  713.                     MyWebClient client = new MyWebClient(); 
  714.   
  715.                     client.UploadProgressChanged += new UploadProgressChangedEventHandler( client_UploadProgressChanged ); 
  716.                     client.UploadFileCompleted += new UploadFileCompletedEventHandler( client_UploadFileCompleted ); 
  717.                     client.Credentials = new NetworkCredential( this.UserName, this.Password ); 
  718.                     if( this.Proxy != null
  719.                     { 
  720.                         client.Proxy = this.Proxy; 
  721.                     } 
  722.                     client.UploadFileAsync( new Uri( this.Uri.ToString() + RemoteFileName ), LocalFullPath ); 
  723.   
  724.                 } 
  725.                 else 
  726.                 { 
  727.                     throw new Exception( "本地文件不存在!" ); 
  728.                 } 
  729.             } 
  730.             catch( Exception ep ) 
  731.             { 
  732.                 ErrorMsg = ep.ToString(); 
  733.                 throw ep; 
  734.             } 
  735.         } 
  736.         /// <summary> 
  737.         /// 异步上传文件到FTP服务器 
  738.         /// </summary> 
  739.         /// <param name="FileBytes">上传的二进制数据</param> 
  740.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  741.         public void UploadFileAsync( byte[] FileBytes, string RemoteFileName ) 
  742.         { 
  743.             if( !IsValidFileChars( RemoteFileName ) ) 
  744.             { 
  745.                 throw new Exception( "非法文件名或目录名!" ); 
  746.             } 
  747.             UploadFileAsync( FileBytes, RemoteFileName, false ); 
  748.         } 
  749.         /// <summary> 
  750.         /// 异步上传文件到FTP服务器 
  751.         /// </summary> 
  752.         /// <param name="FileBytes">文件二进制内容</param> 
  753.         /// <param name="RemoteFileName">要在FTP服务器上面保存文件名</param> 
  754.         /// <param name="OverWriteRemoteFile">是否覆盖远程服务器上面同名的文件</param> 
  755.         public void UploadFileAsync( byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile ) 
  756.         { 
  757.             try 
  758.             { 
  759.   
  760.                 if( !IsValidFileChars( RemoteFileName ) ) 
  761.                 { 
  762.                     throw new Exception( "非法文件名!" ); 
  763.                 } 
  764.                 if( !OverWriteRemoteFile && FileExist( RemoteFileName ) ) 
  765.                 { 
  766.                     throw new Exception( "FTP服务上面已经存在同名文件!" ); 
  767.                 } 
  768.                 string TempPath = System.Environment.GetFolderPath( Environment.SpecialFolder.Templates ); 
  769.                 if( !TempPath.EndsWith( "/" ) ) 
  770.                 { 
  771.                     TempPath += "/"
  772.                 } 
  773.                 string TempFile = TempPath + Path.GetRandomFileName(); 
  774.                 TempFile = Path.ChangeExtension( TempFile, Path.GetExtension( RemoteFileName ) ); 
  775.                 FileStream Stream = new FileStream( TempFile, FileMode.CreateNew, FileAccess.Write ); 
  776.                 Stream.Write( FileBytes, 0, FileBytes.Length );   //注意,因为Int32的最大限制,最大上传文件只能是大约2G多一点 
  777.                 Stream.Flush(); 
  778.                 Stream.Close(); 
  779.                 Stream.Dispose(); 
  780.                 _isDeleteTempFile = true
  781.                 _UploadTempFile = TempFile; 
  782.                 FileBytes = null
  783.                 UploadFileAsync( TempFile, RemoteFileName, OverWriteRemoteFile ); 
  784.   
  785.   
  786.   
  787.             } 
  788.             catch( Exception ep ) 
  789.             { 
  790.                 ErrorMsg = ep.ToString(); 
  791.                 throw ep; 
  792.             } 
  793.         } 
  794.   
  795.         /// <summary> 
  796.         /// 异步上传文件完成之后触发的事件 
  797.         /// </summary> 
  798.         /// <param name="sender">下载对象</param> 
  799.         /// <param name="e">数据信息对象</param> 
  800.         void client_UploadFileCompleted( object sender, UploadFileCompletedEventArgs e ) 
  801.         { 
  802.             if( _isDeleteTempFile ) 
  803.             { 
  804.                 if( File.Exists( _UploadTempFile ) ) 
  805.                 { 
  806.                     File.SetAttributes( _UploadTempFile, FileAttributes.Normal ); 
  807.                     File.Delete( _UploadTempFile ); 
  808.                 } 
  809.                 _isDeleteTempFile = false
  810.             } 
  811.             if( UploadFileCompleted != null
  812.             { 
  813.                 UploadFileCompleted( sender, e ); 
  814.             } 
  815.         } 
  816.   
  817.         /// <summary> 
  818.         /// 异步上传进度发生改变触发的事件 
  819.         /// </summary> 
  820.         /// <param name="sender">下载对象</param> 
  821.         /// <param name="e">进度信息对象</param> 
  822.         void client_UploadProgressChanged( object sender, UploadProgressChangedEventArgs e ) 
  823.         { 
  824.             if( UploadProgressChanged != null
  825.             { 
  826.                 UploadProgressChanged( sender, e ); 
  827.             } 
  828.         } 
  829.         #endregion 
  830.         #region 列出目录文件信息 
  831.         /// <summary> 
  832.         /// 列出FTP服务器上面当前目录的所有文件和目录 
  833.         /// </summary> 
  834.         public FileStruct[] ListFilesAndDirectories() 
  835.         { 
  836.             Response = Open( this.Uri, WebRequestMethods.Ftp.ListDirectoryDetails ); 
  837.             StreamReader stream = new StreamReader( Response.GetResponseStream(), Encoding.Default ); 
  838.             string Datastring = stream.ReadToEnd(); 
  839.             FileStruct[] list = GetList( Datastring ); 
  840.             return list; 
  841.         } 
  842.         /// <summary> 
  843.         /// 列出FTP服务器上面当前目录的所有文件 
  844.         /// </summary> 
  845.         public FileStruct[] ListFiles() 
  846.         { 
  847.             FileStruct[] listAll = ListFilesAndDirectories(); 
  848.             List<FileStruct> listFile = new List<FileStruct>(); 
  849.             foreach( FileStruct file in listAll ) 
  850.             { 
  851.                 if( !file.IsDirectory ) 
  852.                 { 
  853.                     listFile.Add( file ); 
  854.                 } 
  855.             } 
  856.             return listFile.ToArray(); 
  857.         } 
  858.   
  859.         /// <summary> 
  860.         /// 列出FTP服务器上面当前目录的所有的目录 
  861.         /// </summary> 
  862.         public FileStruct[] ListDirectories() 
  863.         { 
  864.             FileStruct[] listAll = ListFilesAndDirectories(); 
  865.             List<FileStruct> listDirectory = new List<FileStruct>(); 
  866.             foreach( FileStruct file in listAll ) 
  867.             { 
  868.                 if( file.IsDirectory ) 
  869.                 { 
  870.                     listDirectory.Add( file ); 
  871.                 } 
  872.             } 
  873.             return listDirectory.ToArray(); 
  874.         } 
  875.         /// <summary> 
  876.         /// 获得文件和目录列表 
  877.         /// </summary> 
  878.         /// <param name="datastring">FTP返回的列表字符信息</param> 
  879.         private FileStruct[] GetList( string datastring ) 
  880.         { 
  881.             List<FileStruct> myListArray = new List<FileStruct>(); 
  882.             string[] dataRecords = datastring.Split( ' 
  883. ' ); 
  884.             FileListStyle _directoryListStyle = GuessFileListStyle( dataRecords ); 
  885.             foreach( string s in dataRecords ) 
  886.             { 
  887.                 if( _directoryListStyle != FileListStyle.Unknown && s != ""
  888.                 { 
  889.                     FileStruct f = new FileStruct(); 
  890.                     f.Name = ".."
  891.                     switch( _directoryListStyle ) 
  892.                     { 
  893.                         case FileListStyle.UnixStyle: 
  894.                             f = ParseFileStructFromUnixStyleRecord( s ); 
  895.                             break
  896.                         case FileListStyle.WindowsStyle: 
  897.                             f = ParseFileStructFromWindowsStyleRecord( s ); 
  898.                             break
  899.                     } 
  900.                     if( !( f.Name == "." || f.Name == ".." ) ) 
  901.                     { 
  902.                         myListArray.Add( f ); 
  903.                     } 
  904.                 } 
  905.             } 
  906.             return myListArray.ToArray(); 
  907.         } 
  908.   
  909.         /// <summary> 
  910.         /// 从Windows格式中返回文件信息 
  911.         /// </summary> 
  912.         /// <param name="Record">文件信息</param> 
  913.         private FileStruct ParseFileStructFromWindowsStyleRecord( string Record ) 
  914.         { 
  915.             FileStruct f = new FileStruct(); 
  916.             string processstr = Record.Trim(); 
  917.             string dateStr = processstr.Substring( 0, 8 ); 
  918.             processstr = ( processstr.Substring( 8, processstr.Length - 8 ) ).Trim(); 
  919.             string timeStr = processstr.Substring( 0, 7 ); 
  920.             processstr = ( processstr.Substring( 7, processstr.Length - 7 ) ).Trim(); 
  921.             DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat; 
  922.             myDTFI.ShortTimePattern = "t"
  923.             f.CreateTime = DateTime.Parse( dateStr + " " + timeStr, myDTFI ); 
  924.             if( processstr.Substring( 0, 5 ) == "<DIR>"
  925.             { 
  926.                 f.IsDirectory = true
  927.                 processstr = ( processstr.Substring( 5, processstr.Length - 5 ) ).Trim(); 
  928.             } 
  929.             else 
  930.             { 
  931.                 string[] strs = processstr.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );   // true); 
  932.                 processstr = strs[ 1 ]; 
  933.                 f.IsDirectory = false
  934.             } 
  935.             f.Name = processstr; 
  936.             return f; 
  937.         } 
  938.   
  939.   
  940.         /// <summary> 
  941.         /// 判断文件列表的方式Window方式还是Unix方式 
  942.         /// </summary> 
  943.         /// <param name="recordList">文件信息列表</param> 
  944.         private FileListStyle GuessFileListStyle( string[] recordList ) 
  945.         { 
  946.             foreach( string s in recordList ) 
  947.             { 
  948.                 if( s.Length > 10 
  949.                  && Regex.IsMatch( s.Substring( 0, 10 ), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)" ) ) 
  950.                 { 
  951.                     return FileListStyle.UnixStyle; 
  952.                 } 
  953.                 else if( s.Length > 8 
  954.                  && Regex.IsMatch( s.Substring( 0, 8 ), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]" ) ) 
  955.                 { 
  956.                     return FileListStyle.WindowsStyle; 
  957.                 } 
  958.             } 
  959.             return FileListStyle.Unknown; 
  960.         } 
  961.   
  962.         /// <summary> 
  963.         /// 从Unix格式中返回文件信息 
  964.         /// </summary> 
  965.         /// <param name="Record">文件信息</param> 
  966.         private FileStruct ParseFileStructFromUnixStyleRecord( string Record ) 
  967.         { 
  968.             FileStruct f = new FileStruct(); 
  969.             string processstr = Record.Trim(); 
  970.             f.Flags = processstr.Substring( 0, 10 ); 
  971.             f.IsDirectory = ( f.Flags[ 0 ] == 'd' ); 
  972.             processstr = ( processstr.Substring( 11 ) ).Trim(); 
  973.             _cutSubstringFromStringWithTrim( ref processstr, ' ', 0 );   //跳过一部分 
  974.             f.Owner = _cutSubstringFromStringWithTrim( ref processstr, ' ', 0 ); 
  975.             f.Group = _cutSubstringFromStringWithTrim( ref processstr, ' ', 0 ); 
  976.             _cutSubstringFromStringWithTrim( ref processstr, ' ', 0 );   //跳过一部分 
  977.             string yearOrTime = processstr.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries )[ 2 ]; 
  978.             if( yearOrTime.IndexOf( ":" ) >= 0 )  //time 
  979.             { 
  980.                 processstr = processstr.Replace( yearOrTime, DateTime.Now.Year.ToString() ); 
  981.             } 
  982.             f.CreateTime = DateTime.Parse( _cutSubstringFromStringWithTrim( ref processstr, ' ', 8 ) ); 
  983.             f.Name = processstr;   //最后就是名称 
  984.             return f; 
  985.         } 
  986.   
  987.         /// <summary> 
  988.         /// 按照一定的规则进行字符串截取 
  989.         /// </summary> 
  990.         /// <param name="s">截取的字符串</param> 
  991.         /// <param name="c">查找的字符</param> 
  992.         /// <param name="startIndex">查找的位置</param> 
  993.         private string _cutSubstringFromStringWithTrim( ref string s, char c, int startIndex ) 
  994.         { 
  995.             int pos1 = s.IndexOf( c, startIndex ); 
  996.             string retString = s.Substring( 0, pos1 ); 
  997.             s = ( s.Substring( pos1 ) ).Trim(); 
  998.             return retString; 
  999.         } 
  1000.         #endregion 
  1001.         #region 目录或文件存在的判断 
  1002.         /// <summary> 
  1003.         /// 判断当前目录下指定的子目录是否存在 
  1004.         /// </summary> 
  1005.         /// <param name="RemoteDirectoryName">指定的目录名</param> 
  1006.         public bool DirectoryExist( string RemoteDirectoryName ) 
  1007.         { 
  1008.             try 
  1009.             { 
  1010.                 if( !IsValidPathChars( RemoteDirectoryName ) ) 
  1011.                 { 
  1012.                     throw new Exception( "目录名非法!" ); 
  1013.                 } 
  1014.                 FileStruct[] listDir = ListDirectories(); 
  1015.                 foreach( FileStruct dir in listDir ) 
  1016.                 { 
  1017.                     if( dir.Name == RemoteDirectoryName ) 
  1018.                     { 
  1019.                         return true
  1020.                     } 
  1021.                 } 
  1022.                 return false
  1023.             } 
  1024.             catch( Exception ep ) 
  1025.             { 
  1026.                 ErrorMsg = ep.ToString(); 
  1027.                 throw ep; 
  1028.             } 
  1029.         } 
  1030.         /// <summary> 
  1031.         /// 判断一个远程文件是否存在服务器当前目录下面 
  1032.         /// </summary> 
  1033.         /// <param name="RemoteFileName">远程文件名</param> 
  1034.         public bool FileExist( string RemoteFileName ) 
  1035.         { 
  1036.             try 
  1037.             { 
  1038.                 if( !IsValidFileChars( RemoteFileName ) ) 
  1039.                 { 
  1040.                     throw new Exception( "文件名非法!" ); 
  1041.                 } 
  1042.                 FileStruct[] listFile = ListFiles(); 
  1043.                 foreach( FileStruct file in listFile ) 
  1044.                 { 
  1045.                     if( file.Name == RemoteFileName ) 
  1046.                     { 
  1047.                         return true
  1048.                     } 
  1049.                 } 
  1050.                 return false
  1051.             } 
  1052.             catch( Exception ep ) 
  1053.             { 
  1054.                 ErrorMsg = ep.ToString(); 
  1055.                 throw ep; 
  1056.             } 
  1057.         } 
  1058.         #endregion 
  1059.         #region 删除文件 
  1060.         /// <summary> 
  1061.         /// 从FTP服务器上面删除一个文件 
  1062.         /// </summary> 
  1063.         /// <param name="RemoteFileName">远程文件名</param> 
  1064.         public void DeleteFile( string RemoteFileName ) 
  1065.         { 
  1066.             try 
  1067.             { 
  1068.                 if( !IsValidFileChars( RemoteFileName ) ) 
  1069.                 { 
  1070.                     throw new Exception( "文件名非法!" ); 
  1071.                 } 
  1072.                 Response = Open( new Uri( this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.DeleteFile ); 
  1073.             } 
  1074.             catch( Exception ep ) 
  1075.             { 
  1076.                 ErrorMsg = ep.ToString(); 
  1077.                 throw ep; 
  1078.             } 
  1079.         } 
  1080.         #endregion 
  1081.         #region 重命名文件 
  1082.         /// <summary> 
  1083.         /// 更改一个文件的名称或一个目录的名称 
  1084.         /// </summary> 
  1085.         /// <param name="RemoteFileName">原始文件或目录名称</param> 
  1086.         /// <param name="NewFileName">新的文件或目录的名称</param> 
  1087.         public bool ReName( string RemoteFileName, string NewFileName ) 
  1088.         { 
  1089.             try 
  1090.             { 
  1091.                 if( !IsValidFileChars( RemoteFileName ) || !IsValidFileChars( NewFileName ) ) 
  1092.                 { 
  1093.                     throw new Exception( "文件名非法!" ); 
  1094.                 } 
  1095.                 if( RemoteFileName == NewFileName ) 
  1096.                 { 
  1097.                     return true
  1098.                 } 
  1099.                 if( FileExist( RemoteFileName ) ) 
  1100.                 { 
  1101.                     Request = OpenRequest( new Uri( this.Uri.ToString() + RemoteFileName ), WebRequestMethods.Ftp.Rename ); 
  1102.                     Request.RenameTo = NewFileName; 
  1103.                     Response = ( FtpWebResponse ) Request.GetResponse(); 
  1104.   
  1105.                 } 
  1106.                 else 
  1107.                 { 
  1108.                     throw new Exception( "文件在服务器上不存在!" ); 
  1109.                 } 
  1110.                 return true
  1111.             } 
  1112.             catch( Exception ep ) 
  1113.             { 
  1114.                 ErrorMsg = ep.ToString(); 
  1115.                 throw ep; 
  1116.             } 
  1117.         } 
  1118.         #endregion 
  1119.         #region 拷贝、移动文件 
  1120.         /// <summary> 
  1121.         /// 把当前目录下面的一个文件拷贝到服务器上面另外的目录中,注意,拷贝文件之后,当前工作目录还是文件原来所在的目录 
  1122.         /// </summary> 
  1123.         /// <param name="RemoteFile">当前目录下的文件名</param> 
  1124.         /// <param name="DirectoryName">新目录名称。 
  1125.         /// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ; 
  1126.         /// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2 
  1127.         /// </param> 
  1128.         /// <returns></returns> 
  1129.         public bool CopyFileToAnotherDirectory( string RemoteFile, string DirectoryName ) 
  1130.         { 
  1131.             string CurrentWorkDir = this.DirectoryPath; 
  1132.             try 
  1133.             { 
  1134.                 byte[] bt = DownloadFile( RemoteFile ); 
  1135.                 GotoDirectory( DirectoryName ); 
  1136.                 bool Success = UploadFile( bt, RemoteFile, false ); 
  1137.                 this.DirectoryPath = CurrentWorkDir; 
  1138.                 return Success; 
  1139.             } 
  1140.             catch( Exception ep ) 
  1141.             { 
  1142.                 this.DirectoryPath = CurrentWorkDir; 
  1143.                 ErrorMsg = ep.ToString(); 
  1144.                 throw ep; 
  1145.             } 
  1146.         } 
  1147.         /// <summary> 
  1148.         /// 把当前目录下面的一个文件移动到服务器上面另外的目录中,注意,移动文件之后,当前工作目录还是文件原来所在的目录 
  1149.         /// </summary> 
  1150.         /// <param name="RemoteFile">当前目录下的文件名</param> 
  1151.         /// <param name="DirectoryName">新目录名称。 
  1152.         /// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ; 
  1153.         /// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2 
  1154.         /// </param> 
  1155.         /// <returns></returns> 
  1156.         public bool MoveFileToAnotherDirectory( string RemoteFile, string DirectoryName ) 
  1157.         { 
  1158.             string CurrentWorkDir = this.DirectoryPath; 
  1159.             try 
  1160.             { 
  1161.                 if( DirectoryName == ""
  1162.                     return false
  1163.                 if( !DirectoryName.StartsWith( "/" ) ) 
  1164.                     DirectoryName = "/" + DirectoryName; 
  1165.                 if( !DirectoryName.EndsWith( "/" ) ) 
  1166.                     DirectoryName += "/"
  1167.                 bool Success = ReName( RemoteFile, DirectoryName + RemoteFile ); 
  1168.                 this.DirectoryPath = CurrentWorkDir; 
  1169.                 return Success; 
  1170.             } 
  1171.             catch( Exception ep ) 
  1172.             { 
  1173.                 this.DirectoryPath = CurrentWorkDir; 
  1174.                 ErrorMsg = ep.ToString(); 
  1175.                 throw ep; 
  1176.             } 
  1177.         } 
  1178.         #endregion 
  1179.         #region 建立、删除子目录 
  1180.         /// <summary> 
  1181.         /// 在FTP服务器上当前工作目录建立一个子目录 
  1182.         /// </summary> 
  1183.         /// <param name="DirectoryName">子目录名称</param> 
  1184.         public bool MakeDirectory( string DirectoryName ) 
  1185.         { 
  1186.             try 
  1187.             { 
  1188.                 if( !IsValidPathChars( DirectoryName ) ) 
  1189.                 { 
  1190.                     throw new Exception( "目录名非法!" ); 
  1191.                 } 
  1192.                 if( DirectoryExist( DirectoryName ) ) 
  1193.                 { 
  1194.                     throw new Exception( "服务器上面已经存在同名的文件名或目录名!" ); 
  1195.                 } 
  1196.                 string a = this.Uri.ToString(); 
  1197.                 string b = DirectoryName; 
  1198.                 Response = Open( new Uri( this.Uri.ToString() + DirectoryName ), WebRequestMethods.Ftp.MakeDirectory ); 
  1199.                 return true
  1200.             } 
  1201.             catch( Exception ep ) 
  1202.             { 
  1203.                 ErrorMsg = ep.ToString(); 
  1204.                 throw ep; 
  1205.             } 
  1206.         } 
  1207.         /// <summary> 
  1208.         /// 从当前工作目录中删除一个子目录 
  1209.         /// </summary> 
  1210.         /// <param name="DirectoryName">子目录名称</param> 
  1211.         public bool RemoveDirectory( string DirectoryName ) 
  1212.         { 
  1213.             try 
  1214.             { 
  1215.                 if( !IsValidPathChars( DirectoryName ) ) 
  1216.                 { 
  1217.                     throw new Exception( "目录名非法!" ); 
  1218.                 } 
  1219.                 if( !DirectoryExist( DirectoryName ) ) 
  1220.                 { 
  1221.                     throw new Exception( "服务器上面不存在指定的文件名或目录名!" ); 
  1222.                 } 
  1223.                 Response = Open( new Uri( this.Uri.ToString() + DirectoryName ), WebRequestMethods.Ftp.RemoveDirectory ); 
  1224.                 return true
  1225.             } 
  1226.             catch( Exception ep ) 
  1227.             { 
  1228.                 ErrorMsg = ep.ToString(); 
  1229.                 throw ep; 
  1230.             } 
  1231.         } 
  1232.         #endregion 
  1233.         #region 文件、目录名称有效性判断 
  1234.         /// <summary> 
  1235.         /// 判断目录名中字符是否合法 
  1236.         /// </summary> 
  1237.         /// <param name="DirectoryName">目录名称</param> 
  1238.         public bool IsValidPathChars( string DirectoryName ) 
  1239.         { 
  1240.             char[] invalidPathChars = Path.GetInvalidPathChars(); 
  1241.             char[] DirChar = DirectoryName.ToCharArray(); 
  1242.             foreach( char C in DirChar ) 
  1243.             { 
  1244.                 if( Array.BinarySearch( invalidPathChars, C ) >= 0 ) 
  1245.                 { 
  1246.                     return false
  1247.                 } 
  1248.             } 
  1249.             return true
  1250.         } 
  1251.         /// <summary> 
  1252.         /// 判断文件名中字符是否合法 
  1253.         /// </summary> 
  1254.         /// <param name="FileName">文件名称</param> 
  1255.         public bool IsValidFileChars( string FileName ) 
  1256.         { 
  1257.             char[] invalidFileChars = Path.GetInvalidFileNameChars(); 
  1258.             char[] NameChar = FileName.ToCharArray(); 
  1259.             foreach( char C in NameChar ) 
  1260.             { 
  1261.                 if( Array.BinarySearch( invalidFileChars, C ) >= 0 ) 
  1262.                 { 
  1263.                     return false
  1264.                 } 
  1265.             } 
  1266.             return true
  1267.         } 
  1268.         #endregion 
  1269.         #region 目录切换操作 
  1270.         /// <summary> 
  1271.         /// 进入一个目录 
  1272.         /// </summary> 
  1273.         /// <param name="DirectoryName"> 
  1274.         /// 新目录的名字。 
  1275.         /// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ; 
  1276.         /// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2 
  1277.         /// </param> 
  1278.         public bool GotoDirectory( string DirectoryName ) 
  1279.         { 
  1280.             string CurrentWorkPath = this.DirectoryPath; 
  1281.             try 
  1282.             { 
  1283.                 DirectoryName = DirectoryName.Replace( "/", "/" ); 
  1284.                 string[] DirectoryNames = DirectoryName.Split( new char[] { '/' } ); 
  1285.                 if( DirectoryNames[ 0 ] == "."
  1286.                 { 
  1287.                     this.DirectoryPath = "/"
  1288.                     if( DirectoryNames.Length == 1 ) 
  1289.                     { 
  1290.                         return true
  1291.                     } 
  1292.                     Array.Clear( DirectoryNames, 0, 1 ); 
  1293.                 } 
  1294.                 bool Success = false
  1295.                 foreach( string dir in DirectoryNames ) 
  1296.                 { 
  1297.                     if( dir != null
  1298.                     { 
  1299.                         Success = EnterOneSubDirectory( dir ); 
  1300.                         if( !Success ) 
  1301.                         { 
  1302.                             this.DirectoryPath = CurrentWorkPath; 
  1303.                             return false
  1304.                         } 
  1305.                     } 
  1306.                 } 
  1307.                 return Success; 
  1308.   
  1309.             } 
  1310.             catch( Exception ep ) 
  1311.             { 
  1312.                 this.DirectoryPath = CurrentWorkPath; 
  1313.                 ErrorMsg = ep.ToString(); 
  1314.                 throw ep; 
  1315.             } 
  1316.         } 
  1317.         /// <summary> 
  1318.         /// 从当前工作目录进入一个子目录 
  1319.         /// </summary> 
  1320.         /// <param name="DirectoryName">子目录名称</param> 
  1321.         private bool EnterOneSubDirectory( string DirectoryName ) 
  1322.         { 
  1323.             try 
  1324.             { 
  1325.                 if( DirectoryName.IndexOf( "/" ) >= 0 || !IsValidPathChars( DirectoryName ) ) 
  1326.                 { 
  1327.                     throw new Exception( "目录名非法!" ); 
  1328.                 } 
  1329.                 if( DirectoryName.Length > 0 && DirectoryExist( DirectoryName ) ) 
  1330.                 { 
  1331.                     if( !DirectoryName.EndsWith( "/" ) ) 
  1332.                     { 
  1333.                         DirectoryName += "/"
  1334.                     } 
  1335.                     _DirectoryPath += DirectoryName; 
  1336.                     return true
  1337.                 } 
  1338.                 else 
  1339.                 { 
  1340.                     return false
  1341.                 } 
  1342.             } 
  1343.             catch( Exception ep ) 
  1344.             { 
  1345.                 ErrorMsg = ep.ToString(); 
  1346.                 throw ep; 
  1347.             } 
  1348.         } 
  1349.         /// <summary> 
  1350.         /// 从当前工作目录往上一级目录 
  1351.         /// </summary> 
  1352.         public bool ComeoutDirectory() 
  1353.         { 
  1354.             if( _DirectoryPath == "/"
  1355.             { 
  1356.                 ErrorMsg = "当前目录已经是根目录!"
  1357.                 throw new Exception( "当前目录已经是根目录!" ); 
  1358.             } 
  1359.             char[] sp = new char[ 1 ] { '/' }; 
  1360.   
  1361.             string[] strDir = _DirectoryPath.Split( sp, StringSplitOptions.RemoveEmptyEntries ); 
  1362.             if( strDir.Length == 1 ) 
  1363.             { 
  1364.                 _DirectoryPath = "/"
  1365.             } 
  1366.             else 
  1367.             { 
  1368.                 _DirectoryPath = String.Join( "/", strDir, 0, strDir.Length - 1 ); 
  1369.             } 
  1370.             return true
  1371.   
  1372.         } 
  1373.         #endregion 
  1374.         #region 重载WebClient,支持FTP进度 
  1375.         internal class MyWebClient : WebClient 
  1376.         { 
  1377.             protected override WebRequest GetWebRequest( Uri address ) 
  1378.             { 
  1379.                 FtpWebRequest req = ( FtpWebRequest ) base.GetWebRequest( address ); 
  1380.                 req.UsePassive = false
  1381.                 return req; 
  1382.             } 
  1383.         } 
  1384.         #endregion 
  1385.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值