Click here to Skip to main content
13,631,113 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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);
}


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 27-Aug-13 2:05am
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,
ankit.agg1984 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.
ankit.agg1984 27-Aug-13 8:30am
   
My system's windows firewall also disabled.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

(The second solution also provided a nice way to locate the specific error)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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;
}


Cheers,
Edo
  Permalink  
v2
Comments
Herbisaurus 27-Aug-13 9:27am
   
*****
Joezer BH 27-Aug-13 9:28am
   
Thank you!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
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
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180712.1 | Last Updated 29 May 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100