Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hello,

I am writing this code for .txt files download:-
C#
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);
}


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
Posted
Updated 27-Aug-13 2:28am
v3
Comments
Prasad Khandekar 27-Aug-13 8:16am    
Hello Ankit,

May be your FTP server is configured in such a way as to block the annonymous access. try supplying the credentials in ReadBOMFileList as well.

Regards,
[no name] 27-Aug-13 8:20am    
I have a right credentials.
sid2x 27-Aug-13 8:28am    
Check whether your application has proper access to Internet, FTP involves receiving files so Windows Firewall or any other antivirus may be blocking it. Try disable them. Also specify any kind of debug log if possible.
[no name] 27-Aug-13 8:30am    
My system's windows firewall also disabled.

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.
 
Share this answer
 
Comments
Herbisaurus 27-Aug-13 9:27am    
*****

(The second solution also provided a nice way to locate the specific error)
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

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


Cheers,
Edo
 
Share this answer
 
v2
Comments
Herbisaurus 27-Aug-13 9:27am    
*****
Joezer BH 27-Aug-13 9:28am    
Thank you!
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
 
Share this answer
 
v2
Comments
CHill60 28-May-14 8:46am    
Incorrect. "/" is not the escape character "\" is. The OP used strings in the format @"\some\text" i.e. verbatim
Member 2506838 4-Oct-17 4:32am    
very useful solution for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900