Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

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: WINSCP SFTP CLIENT UPLOAD USING C# not working as a SERVICE

Please read FAQ. If that does not help, come back.
venkatt

WINSCP SFTP CLIENT UPLOAD USING C# not working as a SERVICE

Hi Support,

I have created C# script to upload files to remote server. If i run exe is working fine.
My problem is, i am trying to create a service using two ways, It is not working in either way.
1) By using XYNTService, i am trying to call my C# application exe. for example my project name "winscpservice". i am trying to install winscpservice.exe using XYNTService.It is not uploading my file. Please suggest the problem.
2) Created own Service using Windows Service option, still not working.

here is my code:
class Program
{
const string logname = "log.xml";


static void Main(string[] args)
{
int fileExist = 0;
string fileName = null;
string fileName_rename = null;
while (true)
{
fileName = null;
//check file present : EZL_REDEEM or ATR_
//C:\Users\venkat\Desktop\
DirectoryInfo dir = new DirectoryInfo("C:\\Users\\venkat\\Desktop");//Directory.getCurrentDirectory());
foreach (FileInfo f in dir.GetFiles("exam*.txt"))
{
string curFile = f.ToString();
//string curFile

curFile = dir.ToString() + "\\" + curFile;
Console.WriteLine(File.Exists(curFile) ? fileExist = 1 : fileExist = 0);
// Console.WriteLine(File.Exists() ? "File exists." : "File does not exist.");

//string curFile = @"C:\Users\venkat\Desktop\*.txt";
//Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
/// Run hidden WinSCP process
if (fileExist == 1)
{
Process winscp = new Process();
winscp.StartInfo.FileName = "winscp.com";
winscp.StartInfo.Arguments = "/log=" + logname;
winscp.StartInfo.UseShellExecute = false;
winscp.StartInfo.RedirectStandardInput = true;
winscp.StartInfo.RedirectStandardOutput = true;
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://venkat:1111@193.168.10.50:69 -privatekey=\"C:\\Documents and Settings\\Administrator\\Desktop\\key.ppk\"");
//winscp.StandardInput.WriteLine("ls");

fileName_rename = dir.ToString() + "\\" + "ren" + f.ToString();
// fileName_backup = dir.ToString() +"\\" + "temp"+"\\"+f.ToString();
File.Move(curFile, fileName_rename);
fileName = "put " + fileName_rename;// +" ren" + f.ToString();
winscp.StandardInput.WriteLine(fileName);
fileName = null;
fileName = "mv " + "ren" + f.ToString() + " " + f.ToString();
winscp.StandardInput.WriteLine(fileName);
winscp.StandardInput.Close();


/// Collect all output (not used in this example)
string output = winscp.StandardOutput.ReadToEnd();

/// 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)
XPathDocument log = new XPathDocument(logname);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("w", "https://winscp.net/schema/session/1.0");
XPathNavigator nav = log.CreateNavigator();

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

/// See if there are any messages associated with the error
foreach (XPathNavigator message in nav.Select("//w:message", ns))
{
Console.WriteLine(message.Value);
}
}
else
{
/// It can be worth looking for directory listing even in case of
/// error as possibly only upload may fail
File.Delete(curFile);
XPathNodeIterator files = nav.Select("//w:file", ns);
Console.WriteLine(string.Format("There are {0} files and subdirectories:", files.Count));
foreach (XPathNavigator file in files)
{
Console.WriteLine(file.SelectSingleNode("w:filename/@value", ns).Value);
}
}
}
}
}
}
}

Please give me a suggestion, How to run scripts as service.
Thanks a lot.

~Venkat