I am trying to download a file to server. I passed username and key and connection is working perfectly. But when I download the file then it is not downloaded successfully and file name is also changed.I do not know why it happens.
public bool GetSFTP()
{
string filename = string.Empty, outfilename = null, output = null, errmsg = null;
// Run hidden WinSCP process
Process winscp = new Process();
string logname = Path.ChangeExtension(Path.GetTempFileName(), "xml");
var _with1 = winscp.StartInfo;
// SFTPExecutable needs to be defined in app.config to point to winscp.com
try
{
_with1.FileName = ConfigurationSettings.AppSettings["SFTPExecutable"].ToString();
if (_with1.FileName == null || _with1.FileName.Length == 0)
{
EventLog.WriteEntry(logSource, "from GetSFTP: SFTPExecutable not set in config file.", EventLogEntryType.Warning);
throw (new Exception("from GetSFTP: SFTPExecutable not set in config file."));
}
}
catch (Exception ex)
{
errmsg = ex.Message;
EventLog.WriteEntry(logSource, errmsg, EventLogEntryType.Error);
return false;
}
_with1.Arguments = "/log=" + logname;
_with1.UseShellExecute = false;
_with1.RedirectStandardInput = true;
_with1.RedirectStandardOutput = true;
_with1.CreateNoWindow = true;
try
{
winscp.Start();
}
catch (Exception ex)
{
EventLog.WriteEntry(logSource, "from GetSFTP: Could not run the WinSCP executable.", EventLogEntryType.FailureAudit);
errmsg = "from GetSFTP: Could not run the WinSCP executable " + winscp.StartInfo.FileName + Environment.NewLine + ex.Message;
return false;
}
// Feed in the scripting commands
var _with2 = winscp.StandardInput;
_with2.WriteLine("option batch abort");
_with2.WriteLine("option confirm off");
//_with2.WriteLine("open sftp://" + username + ":" + password + "@" + remotehost);
_with2.WriteLine(" open " + Protocol + "://" + Username + "@" + Hostname + ":" + Port + " -privatekey=\"" + PublicKeyPath + "\"");
_with2.WriteLine("cd /entitlement/ENT_7005");
//_with2.WriteLine("put C:\\Ahsan_Test\\fwfi.log \"" + defaultRmDir + "\"");
//_with2.WriteLine("exit");
/*
filename = "C:\\Ahsan_Test\\fwfi.log";
if (outfilename == null)
_with2.WriteLine("put " + filename);
else
_with2.WriteLine("put " + filename + " \"" + outfilename + "\"");
*/
_with2.WriteLine("ls");
_with2.WriteLine("get \"fwis.xml\" \"" + defaultLocalDir + "\"");
_with2.Close();
//Get winscp output
//if ((output != null))
output = winscp.StandardOutput.ReadToEnd();
MessageBox.Show(output);
// Wait until WinSCP finishes
winscp.WaitForExit();
// Parse and interpret the XML log
// (Note that in case of fatal failure the log file may not exist at all)
if (!File.Exists(logname))
{
errmsg = "from GetSFTP: The WinSCP executable appears to have crashed.";
EventLog.WriteEntry(logSource,errmsg, EventLogEntryType.FailureAudit);
return false;
}
XPathDocument log = new XPathDocument(logname);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("w", "http://winscp.net/schema/session/1.0");
XPathNavigator nav = log.CreateNavigator();
// Success (0) or error?
bool status = (winscp.ExitCode == 0);
MessageBox.Show(status.ToString());
if (!status)
{
errmsg = "from GetSFTP: There was an error transferring " + filename + ".";
// See if there are any messages associated with the error
foreach (XPathNavigator message in nav.Select("//w:message", ns))
{
errmsg += Environment.NewLine + message.Value;
}
}
try
{
File.Delete(logname);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "SFTP", MessageBoxButtons.OK, MessageBoxIcon.Error);
EventLog.WriteEntry(logSource, ex.Message, EventLogEntryType.FailureAudit);
// at least we tried to clean up
}
return !status;
}