Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Problem in C#.Net with StandardInput.WriteLine

I do not see anything obviously wrong. Can you add /log command-line parameter and post the log file?
Neshfi

Problem in C#.Net with StandardInput.WriteLine

Hello,

I got a weird problem and something tells me its very easy, but I can`t lay my finger on it. The problem is that when I use the StandardInput.WriteLine function the process never exits. Example code:

// Run hidden WinSCP process

Process winscp = new Process();
winscp.StartInfo.FileName = "winscp.com";
//winscp.StartInfo.Arguments = "/script=testscript.txt ";
winscp.StartInfo.UseShellExecute = false;
winscp.StartInfo.RedirectStandardInput = true;
winscp.StartInfo.RedirectStandardOutput = false;
winscp.StartInfo.RedirectStandardError = false;
winscp.StartInfo.CreateNoWindow = true;
winscp.Start();
           
// Feed in the scripting commands
winscp.StandardInput.WriteLine("option batch abort");
winscp.StandardInput.WriteLine("option confirm off");
winscp.StandardInput.WriteLine("open sftp://user:password@srv.somedomain.com -hostkey=\"ssh-rsa xxxx xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx\"");
winscp.StandardInput.WriteLine("cd /sftp");
winscp.StandardInput.WriteLine("lcd C:\\test");
winscp.StandardInput.WriteLine("get test2.txt");
winscp.StandardInput.WriteLine("put -permissions=777 test1.txt");
winscp.StandardInput.WriteLine("exit");
winscp.StandardInput.Close();

winscp.WaitForExit();

// Success (0) or error?
if (winscp.ExitCode != 0)
{
    Console.WriteLine("Error occured");
}
else
{
    Console.WriteLine("Success!");
}


If I execute it like this, then it will keep hanging on the WaitForExit function, but if I would comment all the StandardInput lines and uncomment the StartInfo.Arguments line, then it will work perfectly fine.

Sadly I am in a position in which calling a script is out of the question.