【问题标题】:C# ftp Error 550C# ftp 错误 550
【发布时间】:2013-07-04 18:38:39
【问题描述】:

我现在正在使用FtpWebRequest 创建目录,我会得到像ftp error 550: File unavailable 这样的异常。虽然有时我可以成功创建目录,但我总是得到这个异常。

下面是我的 CheckDir 函数:

protected string CheckDir(string fullpath, string ip, string acc, string pwd)
{
    string[] path = fullpath.Split(slash[1]);

    bool result = false;

    FtpWebRequest request = (FtpWebRequest)(WebRequest.Create(ip + path[2]));
    request.Credentials = new NetworkCredential(acc, pwd);
    request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    request.Timeout = 10000;

    try
    {
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            result = true;
        }
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;

        if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            request = (FtpWebRequest)WebRequest.Create(ip + path[2]);
            request.Credentials = new NetworkCredential(acc, pwd);
            request.Method = WebRequestMethods.Ftp.MakeDirectory;
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
            request.GetResponse();
            result = true;
        }
        else
        {
            result = false;
        }
    }

    if (result == true)
        return path[2];
    else
        return null;
}

【问题讨论】:

    标签: c# ftp


    【解决方案1】:

    我发现我找到了检查现有目录的方法。然后,我使用ContainsListDirecotyDetails

    这是我的功能:

    protected string CheckDir(string fullpath, string c_ip, string c_acc, string c_pwd)
            {
                string[] path = fullpath.Split(slash[1]);
    
                bool result = false;
    
                try
                {
                    if (WebRequestMethods.Ftp.ListDirectoryDetails.Contains(c_ip + path[2]))
                    {
                        result = true;
                    }
                    else
                    {
                        FtpWebRequest request = (FtpWebRequest)(WebRequest.Create(c_ip + path[2]));
                        request.Credentials = new NetworkCredential(c_acc, c_pwd);
                        request.Method = WebRequestMethods.Ftp.MakeDirectory;
                        request.KeepAlive = false;
                        try
                        {
                            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                            response.Close();
                        }
                        catch (Exception)
                        {
                            request.Abort();
                            result = false;
                        }
                        request.Abort();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
    
                if (result == true)
                    return path[2];
                else
                    return null;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多