Esempio n. 1
0
        public void ListFinish(IAsyncResult ar)
        {
            FtpWebResponse response = null;

            try
            {
                ListFileAsyncState stateObj = ar.AsyncState as ListFileAsyncState;
                FtpWebRequest      request  = stateObj.Request;
                _currentPath = stateObj.Path;
                response     = (FtpWebResponse)request.EndGetResponse(ar);
                _result      = response.StatusDescription;
                ServerFiles  = ServerFileInfo.Parse(response.GetResponseStream());
            }
            catch (Exception e)
            {
                if (response != null)
                {
                    _result = response.StatusDescription;
                }
                else
                {
                    _result = e.Message;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                _listFileRequestFinished = true;
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 删除目录
 /// </summary>
 /// <param name="dirName">目录名(含路径)</param>
 /// <returns></returns>
 public bool RemoveDir(string url)
 {
     try
     {
         _deleteFileFinished = false;
         FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
         request.Credentials = new NetworkCredential(this.UserName, this.Password);
         request.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
         FtpWebResponse response = request.GetResponse() as FtpWebResponse;
         IList          fileList = ServerFileInfo.Parse(response.GetResponseStream());
         foreach (ServerFileInfo file in fileList)
         {
             if (file.Name.Equals(".") || file.Name.Equals(".."))
             {
                 continue;
             }
             if (file.Directory)
             {
                 RemoveDir(url + file.Name + "/");
             }
             else
             {
                 if (file.Name.IndexOf('#') != -1)
                 {
                     throw new Exception();
                 }
                 FtpWebRequest delete = (FtpWebRequest)WebRequest.Create(url + file.Name);
                 delete.Credentials = new NetworkCredential(this.UserName, this.Password);
                 delete.Method      = WebRequestMethods.Ftp.DeleteFile;
                 Console.WriteLine(((FtpWebResponse)delete.GetResponse()).StatusDescription);
             }
         }
         request             = (FtpWebRequest)WebRequest.Create(url.Substring(0, url.Length - 1));
         request.Credentials = new NetworkCredential(this.UserName, this.Password);
         request.Method      = WebRequestMethods.Ftp.RemoveDirectory;
         request.KeepAlive   = false;
         response            = request.GetResponse() as FtpWebResponse;
         this.Result         = response.StatusDescription;
         response.Close();
     }
     catch (Exception e)
     {
         this.Result = e.Message;
         return(false);
     }
     finally
     {
         _deleteFileFinished = true;
     }
     return(true);
 }