This project is read-only.

GetFile method throwing an error

Jan 24, 2011 at 8:12 PM

I am trying to download a file using FTPlib, but it throws an error no matter what... Here's the code I've been using:

ftp.SetLocalDirectory(@"D:\New Folder");
            string fullremotepath = @"/folder";
            ftp.SetCurrentDirectory(fullremotepath);
            if (ftp.FileExists(@"/folder/sth.png"))
            {
                MessageBox.Show(@"exists");
                ftp.GetFile(@"/folder/sth.png", true);
            }
            else
            {
                MessageBox.Show(@"doesnt exist");
            }

I get the message that directory exists, but then I get an error: "Access is denied".

I've been searching it for a while, I don't know if there's something I am actually doing wrong...

Any ideas?

Jan 24, 2011 at 8:49 PM

Yeah I am having THE same problem. No idea how to solve it.

Coordinator
Jan 26, 2011 at 8:03 PM

I believe the reason is because the GetFile() method is relative to the current directory, but FileExists() checks from the current path OR the full path (if you start with the root character). Here's your FTP directory structure:

/
|-folder
   |-sth.png

When you first log in, you are at the root: /

When you check that /folder/sth.png exists, it does because that's the full path to the file including the root folder. However, when you change directories, and then try to use GetFile(), you are basically requesting /folder/folder/sth.png, which doesn't exist. Also, make sure the local directory exists and you have write access (D:\New Folder)

Try one of these options:

 

ftp.SetLocalDirectory(@"D:\New Folder");
string fullremotepath = @"/folder";
//ftp.SetCurrentDirectory(fullremotepath); don't change to the directory
if (ftp.FileExists(@"/folder/sth.png"))
{
    MessageBox.Show(@"exists");
    ftp.GetFile(@"/folder/sth.png", true);
}
else
{
    MessageBox.Show(@"doesnt exist");
}

OR:
ftp.SetLocalDirectory(@"D:\New Folder");
string fullremotepath = @"/folder";
ftp.SetCurrentDirectory(fullremotepath);
if (ftp.FileExists(@"/folder/sth.png"))
{
    MessageBox.Show(@"exists");
    ftp.GetFile(@"sth.png", true);
}
else
{
    MessageBox.Show(@"doesnt exist");
}

 

 

Jan 26, 2011 at 8:16 PM

I fixed it before you replied. I had to use 

(@"folder/sth.png")

instead of
(@"/folder/sth.png"))

cause the first "/" should not be there, as folder is not the root directory (ftp account used is not owner account).