extra path getting appeneded while uploading a file in .net

Advertisement

SV999
Guest

extra path getting appeneded while uploading a file in .net

I am using winscp5.1 and I am trying to automate downloading and uploading a file through sharepoint using the winscp .net automation dll. I was able to sucessfully download the file. But while uploading the file, It is appending an extra path to the actual path where the file has to be uploaded. Due to this it fails. This happens in the sharepoint code. I copied the same code to a console application and it runs fine. I don't know why it is appending the extra path in sharepoint.

This is my code present both in sharepoint and in the console app.

SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp, //sftp
HostName = settings.HostName, //hostname
UserName = settings.UserName, //username
Password = settings.Password, //password
SshHostKeyFingerprint = settings.FingerPrint, //fingerprint
PortNumber = settings.Port, //22
};

using (Session _session = new Session())
{
_session.ExecutablePath = settings.WinScpPath; //c:\winscp\winscp.exe

//_session.SessionLogPath = "C:\\SftpDownload\\upoadSessionlog.txt";
//_session.DebugLogPath = "C:\\SftpDownload\\uploadDebuglog.txt";

// Connect

_session.Open(sessionOptions);

if (File.Exists(settings.LocalPath))
{
// Get files
TransferOptions _transferOptions = new TransferOptions();
_transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult _transferResult;
_transferResult = _session.PutFiles(settings.LocalPath, settings.RemotePath, false, _transferOptions);

//setting.localpath= c:\\foo\foo.txt
//settings.Remotepath=/data/projects/foo/

// Throw on any error
_transferResult.Check();

// Print results
_status.Result = true;
_status.UserMessage = String.Format("Uploaded {0}", settings.LocalPath);
}
else
{
_status.Result = false;
_status.ErrorMessage = String.Format("Could not find file {0} or get its attributes. Please verify path/filename", settings.LocalPath);
}


This code works fine in a console app,but when i put this code into sharepoint, then at runtime, when _transferResult.Check() is called, it adds some path extra to the remote path like "/xyz/dummy/ /data/projects/foo/" . As you can see, "/xyz/dummy/ " is the extra path and i get an error

Cannot create remote file '/xyz/dummy/ /data/projects/foo/foo.txt.filepart'


I don't understand how the same code works in console but not in sharepoint. I feel that the path "/xyz/dummy/ " is the home directory of the sftp server. Cansomebody tell me how to get out of this problem? I have tried adding fresh assembly to GAC, referencing dlls afresh and restarting IIS, but no use.
Does the dll cache the path?

Reply with quote

Advertisement

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

Re: extra path getting appeneded while uploading a file in .net

Please attach or email me both Session.SessionLogPath and Session.DebugLogPath

Reply with quote

SV999
Guest

issue fixed

Thanks . This issue is fixed. It seems to be working now and It seems to be because there was a space in the starting of the path mentioned. i.e.
' /data/projects/foo/'. Thanks again.

Reply with quote

cm2pt
Guest

I have an issue when trying to run a powershell script on Task Scheduler to verify the existence of a file in a SFTP server using WINSCP. When I run with the option "Run only when user is logged on" on task scheduler everything runs fine.
The problem comes when I change to the option "run whether user is logged on or not". As soon as I run the script after changing that option I get the following message when doing the session.open command:

"
Exception: WinSCP.SessionLocalException: WinSCP process terminated with exit code 3 and output "", without responding (response log file D:\Logs\WinSCP\LOG_WINSCP_XML_20140324_201253.log was not created). This could indicate lack of write permissions to the log folder or problems starting WinSCP itself.
at WinSCP.Session.Open(SessionOptions sessionOptions)
"

I've tried changing the debug, session and xml log path. It never created the XML log and the session one only creates when the code runs successfully.
I've updated WinSCP to the latest version (5.5.2).

The code I'm using is:
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::SFTP
$sessionOptions.HostName = "***"
$sessionOptions.UserName = "***"
$sessionOptions.Password = "***"
$sessionOptions.SshHostKeyFingerprint = "***"

$session = New-Object WinSCP.Session

$session.DebugLogPath = $WinSCPDebugLogFile
$session.SessionLogPath = $WinSCPSessionLogFile
$session.XMLLogPath = $WinSCPXMLLogFile

"3" | Out-File $LogFile -Append -Force

try
{
# Connect
$session.Open($sessionOptions)

Can you help me understand what the problem is?

Reply with quote

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

cm2pt wrote:

The problem comes when I change to the option "run whether user is logged on or not". As soon as I run the script after changing that option I get the following message when doing the session.open command:

"
Exception: WinSCP.SessionLocalException: WinSCP process terminated with exit code 3 and output "", without responding (response log file D:\Logs\WinSCP\LOG_WINSCP_XML_20140324_201253.log was not created). This could indicate lack of write permissions to the log folder or problems starting WinSCP itself.
at WinSCP.Session.Open(SessionOptions sessionOptions)
Exit code 3 is often caused by this problem:
https://winscp.net/tracker/996
Did you use the assembly often before with old version of WinSCP?
Consider restarting your machine.
Let us know if it does not help.

Reply with quote

Advertisement

cm2pt
Guest

I saw on another thread that a restart was needed after the update of WinSCP. After we did that is works fine.
Can I ask another question on a different topic?

I have a script that goes every 5 minutes check on the server if a file exists. The server closes the connection in the meanwhile and I get the error "Exception: WinSCP.SessionLocalException: Element "group" not found in the log file." when running the FileExists for the third time.
Is there any option to reconnect? I've tried using the Opened property in the Session without any luck.

Reply with quote

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

cm2pt wrote:

I have a script that goes every 5 minutes check on the server if a file exists. The server closes the connection in the meanwhile and I get the error "Exception: WinSCP.SessionLocalException: Element "group" not found in the log file." when running the FileExists for the third time.
The message will get better in the next major release:
https://winscp.net/tracker/1137

Is there any option to reconnect? I've tried using the Opened property in the Session without any luck.
No. You need to create new Session instance.

Reply with quote

cm2pt
Guest

I had to include the below code in the beginning of the while clause to make sure a new session is opened when I want to check for the file existence.
It now works.

# Setup session log files locations
$session = New-Object WinSCP.Session
$session.DebugLogPath = $WinSCPDebugLogFile
$session.SessionLogPath = $WinSCPSessionLogFile
$session.XMLLogPath = $WinSCPXMLLogFile

# Connect
$session.Open($sessionOptions)

Reply with quote

Advertisement

You can post new topics in this forum