为什么在尝试从我的ftp服务器删除文件时,我会遇到异常?

时间:2014-12-20 18:46:02

标签: c# .net winforms ftp treeview

这是我如何选择要从treeView1中删除的文件: 我在菜单选项中右键单击treeView中的文件选择“删除” 然后调用delete方法。

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "Delete")
            {
                delete(treeView1.SelectedNode.FullPath);
            }
        }

这是删除文件的方法:

public void delete(string deleteFile)
        {
            try
            {               
                FtpWebRequest ftpRequest;
                FtpWebResponse ftpResponse;
                string url = "ftp://" + txtHost.Text + "/" + deleteFile;
                url = url.Replace('\\', '/');
                Uri serverUri = new Uri(url);
                ftpRequest = (FtpWebRequest)WebRequest.Create(serverUri);// + "/" + deleteFile);
                ftpRequest.Credentials = new NetworkCredential(UserName, Password);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return;
        }

要删除的文件的路径,因为它是变量deleteFile中的treeView,如下所示:

根\ B \一个新-beginning.jpg

所以我使用的是:url = url.Replace('\','/'); 最后在serverUri中,我看到:ftp://ftp.newsxpressmedia.com/root/B/a-new-beginning.jpg

这是上传文件的正确格式。 但后来我在这条线上得到了例外:

ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

引发WebException:

远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。

System.Net.WebException was caught
  HResult=-2146233079
  Message=The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
  Source=System
  StackTrace:
       at System.Net.FtpWebRequest.CheckError()
       at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
       at System.IO.Stream.Close()
       at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
       at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
       at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
       at System.Net.FtpWebRequest.GetResponse()
       at FTP_ProgressBar.Form1.delete(String deleteFile) in c:\ftp_progressbar\FTP_ProgressBar\Form1.cs:line 665
  InnerException:

第665行是:

ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

也许替换的东西是错的?

这是我的主机ipage.com上的ftp服务器文件管理器的屏幕截图。

MyFtpServer

System.Net.WebException was caught
  HResult=-2146233079
  Message=The underlying connection was closed: An unexpected error occurred on a receive.
  Source=System
  StackTrace:
       at System.Net.FtpWebRequest.CheckError()
       at System.Net.FtpWebRequest.GetResponse()
       at FTP_ProgressBar.Form1.delete(String deleteFile) in c:\ftp_progressbar\FTP_ProgressBar\Form1.cs:line 656
  InnerException:

1 个答案:

答案 0 :(得分:1)

您可能需要将设置更改为ftpRequest.KeepAlive = false;目前设置为true

您可以尝试使用((FtpWebResponse)e.Response).StatusDescription属性从服务器访问实际响应消息,以深入了解特定错误。

try
{
        //Your code
}
catch(WebException e)
{
        String status = ((FtpWebResponse)e.Response).StatusDescription;
}

ftpRequest.UseBinary = true;也可能存在问题。我删除文件时不应该设置这个。只是一个想法。

如果有帮助,请告诉我。

更新代码:

private void Delete(string remoteFile) {
  string deleteRequest = ftpServer + remoteFile;
  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(deleteRequest);
  request.Method = WebRequestMethods.Ftp.DeleteFile;
  request.Credentials = new NetworkCredential(ftpLoginName, ftpLoginPassword);
  request.Proxy = null;
  request.UseBinary = false;
  request.UsePassive = true;
  request.KeepAlive = false;
  FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  Stream responseStream = response.GetResponseStream();
  StreamReader sr = new StreamReader(responseStream);
  sr.ReadToEnd();
  string StatusCode = response.StatusDescription;
  sr.Close();
  response.Close();
}