I’ve been writing an application that allows me to easily deploy files to our corporate website. Since I had to build under the guidelines of my boss and this is my first c# project I needed to do some hacking that most experienced c# programmers would laugh at.
The application does a few things. One it allows you to select any number of files or folders and click a button to automatically add them to zip file via winzip, open an sftp connection with SecureFX, and make an ssh call using plink for additional commands which unzip the file and sync the files across multiple web servers. . I needed a way for my ssh function to wait for my sftp function, and for that to wait for my zip function to complete.
I use the Process.Start method to access the windows command line and pass arguments. Then I used the WaitForExit method to stop any further code from executing until the current process completed. Both are in the System.Diagnostics namespace.
private void ZipIt() { Process oProc = Process.Start("cmd.exe", "/c wzzip.exe -p -r -yb c:\\temp\\corpsite.zip c:\\temp\\filepush\\>c:\\temp\\zip.log"); oProc.WaitForExit(); } private void SFTP() { Process oProc = Process.Start("cmd.exe", "/c sfxcl.exe c:\\temp\\corpsite.zip sftp://user:pass@ip.address>c:\\temp\\sftp.log"); oProc.WaitForExit(); } private void SSH() { Process oProc = Process.Start("cmd.exe", "/c plink.exe -ssh -batch -l user -pw pass ip.address C:\\ppmd\\CorpDeploy\\script\\MoveFiles.bat>c:\\temp\\ssh.log"); oProc.WaitForExit(); }
If you’re more interested in running command line apps and passing arguments to them with c# then you’ll need to copy the exe file to your system32 folder. I still have not figured out a way to hide the command window when these processes run. If you know how to do this please add a comment. Thanks for reading.
No related posts.
Try this
ProcessStartInfo startInfo = new ProcessStartInfo(“cmd.exe”);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = myDirectory; // Directory for cmd file
startInfo.Arguments = “/c mybat.cmd”;
Process.Start(startInfo);
This simple example hides the command window when mybat.cmd is executed. Hope this helps!
Thanks for comment…. It helped me : )
I think this works as well..
StartInfo.CreateNoWindow = true;
C# commandline argument example
http://csharp.net-informations.com/overview/csharp-commandline-arguments.htm
gvg