远程服务器返回错误(550)文件不可用(例如,找不到文件没有访问权限)。 ftp c# [英] the remote server returned an error (550) file unavailable (e.g. file not found no access). ftp c#

查看:229
本文介绍了远程服务器返回错误(550)文件不可用(例如,找不到文件没有访问权限)。 ftp c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在为.txt文件下载这段代码: -

Hello,

I am writing this code for .txt files download:-

public string[] ReadBOMFileList()
{
    string[] mydownloadFiles;
    StringBuilder myresult = new StringBuilder();
    //WebResponse myresponse = null;
    StreamReader myreader = null;
    FtpWebRequest myreqFTP = null;

    try
    {
        FTPSettings.IP = "abc.com/Project/TXN_BAAN/3DS_BOM";
        FTPSettings.UserID = "abc";
        FTPSettings.Password = "abc";
        myreqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/"));
        myreqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        myreqFTP.UseBinary = true;
        myreqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
        FtpWebResponse response = (FtpWebResponse)myreqFTP.GetResponse();
        myreader = new StreamReader(response.GetResponseStream());
        string myline = myreader.ReadLine();
        while (myline != null)
        {
            myresult.Append(myline);
            myresult.Append("\n");
            myline = myreader.ReadLine();
        }
        // to remove the trailing '\n'
        myresult.Remove(myresult.ToString().LastIndexOf('\n'), 1);
        return myresult.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        if (myreader != null)
        {
            myreader.Close();
        }
        mydownloadFiles = null;
        return mydownloadFiles;
    }
}

private void DownloadBOMFile(string file)
{
    try
    {
        FTPSettings.IP = "abc.com/Project/TXN_BAAN";
        FTPSettings.UserID = "abc";
        FTPSettings.Password = "abc";
        string uri = "ftp://" + FTPSettings.IP + "/" + "3DS_BOM" + "/" + file;
        Uri serverUri = new Uri(uri);
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return;
        }
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + "3DS_BOM" + "/" + file));
        reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = false;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream responseStream = response.GetResponseStream();
        if (!Directory.Exists(@"C:\AdminDB3DS_BOM"))
            Directory.CreateDirectory(@"C:\AdminDB3DS_BOM");
        FileStream writeStream = new FileStream(@"C:\AdminDB3DS_BOM\" + file, FileMode.Create);

        int Length = 2048;
        Byte[] buffer = new Byte[Length];
        int bytesRead = responseStream.Read(buffer, 0, Length);
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = responseStream.Read(buffer, 0, Length);
        }
        writeStream.Close();
        response.Close();
    }
    catch (WebException wEx)
    {
        TraceService(wEx.Message.ToString());
    }
    catch (Exception ex)
    {
        TraceService(ex.Message.ToString());
    }
}

public static class FTPSettings
{
    public static string IP { get; set; }
    public static string UserID { get; set; }
    public static string Password { get; set; }
}

string[] BOMfiles = ReadBOMFileList();
foreach (string BOMfile in BOMfiles)
{
    DownloadBOMFile(BOMfile);
}





我的代码收到错误



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



如何修复此错误?



请帮帮我。



先谢谢。



Ankit Agarwal

软件工程师



My code getting error regarding

"the remote server returned an error (550) file unavailable (e.g. file not found no access)."

How will be fixed this error?

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

推荐答案

你要么无法访问你给FtpClient的文件夹/路径中的文件,要么文件不存在。



您必须仔细检查代码构建的文件路径字符串并确保文件存在。如果是,那么你就遇到了一个权限问题,你的代码无法做任何事情。
You either don't have access to the files at the folder/path you gave the FtpClient or the files don't exist.

You'll have to double-check the filepath string your code is building and make sure the file exists. If it does, then you've got a permissions problem that your code cannot do anything about.


你可以尝试使用((FtpWebResponse)e.Response).StatusDescription 属性以深入了解具体错误



You can try to access the actual response message from the server using the ((FtpWebResponse)e.Response).StatusDescription property to get an insight into the specific error

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





干杯,

Edo



Cheers,
Edo


即使我遇到同样的问题,但解决它很简单。你需要处理我们经常错过的转义序列。

使用abc.com//Project//TXN_BAAN//3DS_BOM而不是abc.com/Project/TXN_BAAN/3DS_BOM
Even i faced same issue, But its very simple to fix it. You need to handle the escape sequence, Which we miss often.
use abc.com//Project//TXN_BAAN//3DS_BOM instead abc.com/Project/TXN_BAAN/3DS_BOM


这篇关于远程服务器返回错误(550)文件不可用(例如,找不到文件没有访问权限)。 ftp c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆