C# Session.GetFiles from ftp to Local Drive.

Advertisement

lynzepiper
Joined:
Posts:
32
Location:
London

C# Session.GetFiles from ftp to Local Drive.

I'm trying to download a file from a directory on a FTP site using session.GetFiles method but I keep getting "Can't get file attributes", files does not exist. Before I attempt to download the file I use session.ListDirectory. The file is listed in the RemoteDirectoryInfo that is returned from the session.ListDirectory method. I can see the file in the WinSCP GUI so I know it exists. I have full admin rights to the FTP site. I'm using the following code. The same code works for SFTP but just wont work for FTP or FTPS. I've tried it on 3 different FTP sites, ones which I know work.

[code]
try
{


if (!session.Opened)
Connect(out sessionException, out sslCertificate);

if (session.Opened)
{
bool exists = session.FileExists(remotePath);
TransferOptions transferOption = new TransferOptions();
transferOption.TransferMode = TransferMode.Binary;

transferResult = session.GetFiles("/Outbound/textfile.txt", @"C:\\Local FTP\\", Overwrite, transferOption);
output = session.Output.ToList();
transferResult.Check();

}
}
catch (SessionRemoteException exception)
{

}
finally
{
session.Dispose();
}

return transferResult;
[/code]

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: C# Session.GetFiles from ftp to Local Drive.

Please set Session.SessionLogPath and attach the session log to this thread.

Reply with quote

badik
Guest

Analog Problem GetFiles

> 2012-09-12 18:06:08.774 Script: ls -- "\Document\/"
. 2012-09-12 18:06:08.774 Retrieving directory listing...
> 2012-09-12 18:06:08.774 CWD /Document/
< 2012-09-12 18:06:08.868 250 CWD successful. "/Document" is current directory.
> 2012-09-12 18:06:08.868 PWD
< 2012-09-12 18:06:08.962 257 "/Document" is current directory.
> 2012-09-12 18:06:08.962 TYPE A
< 2012-09-12 18:06:09.055 200 Type set to A
> 2012-09-12 18:06:09.055 PASV
< 2012-09-12 18:06:09.133 227 Entering Passive Mode (10,95,3,14,10,7)
> 2012-09-12 18:06:09.133 MLSD
< 2012-09-12 18:06:09.321 150 Connection accepted
< 2012-09-12 18:06:09.321 226 Transfer OK
. 2012-09-12 18:06:09.321 Directory listing successful
< 2012-09-12 18:06:09.321 Script: D--------- 0 0 ..
< 2012-09-12 18:06:09.321 Script: ---------- 0 138471 Sep 12 10:30:48 2012 fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment
> 2012-09-12 18:06:09.805 Script: get -nopermissions -preservetime -transfer="binary" -- "\Document\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment" "c:\cafk\moy\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment.ufk52.tmp"
. 2012-09-12 18:06:09.805 Listing file "\Document\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment".
. 2012-09-12 18:06:09.805 Retrieving file information...
> 2012-09-12 18:06:09.805 MLST fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment
< 2012-09-12 18:06:09.899 250-Listing /Document/fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment
< 2012-09-12 18:06:09.899 type=file;size=138471;modify=20120912103048; /Document/fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment
< 2012-09-12 18:06:09.915 250 End
. 2012-09-12 18:06:09.915 Retrieving file information successful
< 2012-09-12 18:06:09.915 Script: Can't get attributes of file '\Document\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment'.
< 2012-09-12 18:06:09.915 Script: File or folder '\Document\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment' does not exist.
. 2012-09-12 18:06:09.915 Script: Failed
> 2012-09-12 18:06:09.946 Script: exit
. 2012-09-12 18:06:09.946 Script: Exit code: 1
. 2012-09-12 18:06:09.962 Disconnected from server
. 2012-09-12 18:08:39.881 --------------------------------------------------------------------------
. 2012-09-12 18:08:39.881 WinSCP Version 5.0.9 (Build 2578) (OS 5.1.2600 Service Pack 3)

Reply with quote

badik
Joined:
Posts:
1

Error code :
    // Setup session options
    var sessionOptions = new ActiveXObject("WinSCP.SessionOptions");
    sessionOptions.Protocol = 2;
    sessionOptions.HostName = mHostName
    sessionOptions.UserName = mUserName
    sessionOptions.Password = mPassword
     var session = new ActiveXObject("WinSCP.Session");
 
    try
    {
        // Connect
session.Open(sessionOptions);
session.GetFiles("/Document/MyFile.txt","c:\new\my_File.txt").Check()

    }
catch (e)
{
    alert("Error:" + e.message);
}


temporary solution a change of root RemoteDirectory

    // Setup session options
    var sessionOptions = new ActiveXObject("WinSCP.SessionOptions");
    sessionOptions.Protocol = 2;
    sessionOptions.HostName = mHostName
    sessionOptions.UserName = mUserName
    sessionOptions.Password = mPassword
   

    //temporary solution a change of root RemoteDirectory
    sessionOptions.AddRawSettings("RemoteDirectory","/Document/")
   

    var session = new ActiveXObject("WinSCP.Session");
 
    try
    {
        // Connect
session.Open(sessionOptions);
session.GetFiles("MyFile.txt","c:\new\my_File.txt").Check()

    }
catch (e)
{
    alert("Error:" + e.message);
}

Reply with quote

martin
Site Admin
martin avatar

get -nopermissions -preservetime -transfer="binary" -- "\Document\fe75400f-d260-4d8b-81d4-3c318a7dd568.priority80.gz.shipment"
Please use forward slashes.

Reply with quote

Advertisement

nicis
Joined:
Posts:
5

hi,

does the problem could be solved ?
I use the temporary solution (AddRawSettings("RemoteDirectory") but this solution does not permit to "reset" the remote directory parameter(I get a dictonnary error, element already exist)
1) does the raw setting is used when the session is open or at every "command"
2) can the raw setting can be modified ?
3) does a solution exist in .net&com interface to navigate from one directory to another one (like "cd remotedirectory")
4) does a solution exist to submit a command (like "cd ") to winscp.exe process ?

thanks

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

nicis wrote:

does the problem could be solved ?
I use the temporary solution (AddRawSettings("RemoteDirectory") but this solution does not permit to "reset" the remote directory parameter(I get a dictonnary error, element already exist)
1) does the raw setting is used when the session is open or at every "command"
2) can the raw setting can be modified ?
3) does a solution exist in .net&com interface to navigate from one directory to another one (like "cd remotedirectory")
4) does a solution exist to submit a command (like "cd ") to winscp.exe process ?
I do not see how this is related to this thread.
Anyway, the assembly does not have a concept of current remote working directory. You always need to use absolute paths for remote files. You should not rely on using paths relative to the user's home directory.

Reply with quote

nicis
Joined:
Posts:
5

sorry if I did not created a new thread but It is related to this message since the initial problem I get was the file attributes error

and since the solution was to use AddRawSettings to set the directory ...

that meams also that if I want to check file attributes while working on differents directories, I have to close the session and open it again ?

Reply with quote

Advertisement

a.ilic
Guest

FTP file download, .NET C#

Does anyone have permanent solution for the topic problem? I have the same problem. I can't download the file requesting through full path, but if I set remote directory first and then request the file, download finishes successfully.
. 2012-10-31 20:35:48.392 Connected
. 2012-10-31 20:35:48.392 --------------------------------------------------------------------------
. 2012-10-31 20:35:48.392 Using FTP protocol.
. 2012-10-31 20:35:48.392 Doing startup conversation with host.
> 2012-10-31 20:35:48.397 PWD
< 2012-10-31 20:35:48.462 257 "/" is your current location
. 2012-10-31 20:35:48.462 Getting current directory name.
. 2012-10-31 20:35:48.462 Startup conversation with host finished.
< 2012-10-31 20:35:48.467 Script: Active session: [1] ...
> 2012-10-31 20:35:48.947 Script: get  -nopermissions -preservetime -transfer="binary" -- "/nexchange/download/bestprice.png" "d:\_NIS\download\bestprice.png"
. 2012-10-31 20:35:48.947 Listing file "/nexchange/download/bestprice.png".
. 2012-10-31 20:35:48.947 Retrieving file information...
> 2012-10-31 20:35:48.947 MLST bestprice.png
< 2012-10-31 20:35:49.017 550 Can't check for file existence
. 2012-10-31 20:35:49.017 Could not retrieve file information
< 2012-10-31 20:35:49.017 Script: Can't get attributes of file '/nexchange/download/bestprice.png'.
< 2012-10-31 20:35:49.022 Script: Could not retrieve file information

< 2012-10-31 20:35:49.022 Can't check for file existence
. 2012-10-31 20:35:49.032 Script: Failed
> 2012-10-31 20:35:49.682 Script: exit
. 2012-10-31 20:35:49.682 Script: Exit code: 1
. 2012-10-31 20:35:49.682 Disconnected from server

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: FTP file download, .NET C#

a.ilic wrote:

Does anyone have permanent solution for the topic problem? I have the same problem. I can't download the file requesting through full path, but if I set remote directory first and then request the file, download finishes successfully.
Thanks for your report.

This bug is being tracked already:
https://winscp.net/tracker/926

Reply with quote

swegs
Joined:
Posts:
1

Is it possible to change the Remote Directory without closing and reopening the session?

I'm pretty new to using WinSCP and C# programming in general.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

swegs wrote:

Is it possible to change the Remote Directory without closing and reopening the session?

I'm pretty new to using WinSCP and C# programming in general.
The WinSCP .NET assembly does not have a concept of current remote working directory. You should always use full paths. Anyway, how do you set the remote directory when opening a session?

Reply with quote

Advertisement

You can post new topics in this forum