I am doing two commands in cmd from C# application.
renaming a file
string commandToExecute;
commandToExecute = #"/c ren E:\filename filename.rar";
Process.Start("cmd.exe", commandToExecute);
unrar-ing a file
commandToExecute = #"/c unrar e E:\filename.rar";
Process.Start("cmd.exe", commandToExecute);
The first part of the code works, but the second part doesn't, although it is working when I write the command in cmd manually. And I noticed that when executing in C# it runs the application itself again (like recursion). I don't know why.
Note: I am using 64-bit windows 7.
Remember that Process.Start launches another process. If the commands need to run serially, you need to obtain a Process object from Process.Start and call the WaitForExit method.
Related
I'm developing an application in C#. The main idea is:
Press button
Open .bat file
The .bat file opens Telnet [IP] [Port]
.bat file executes the VBScript
VBScript writes some commands to the telnet window
When I run this batch file by double-clicking on it, it works fine. However, wen I try to run it from a C# app, it doesn't work.
I already tried many methods.
Here is a few examples about what I tried:
Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file?
C# Winforms and command line batch files
batch works fine but "windows cannot find devcon.exe" in c# program
When I tried this:
string cmd = #"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = #"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();
I got the error:
windows cannot find .exe make sure you typed the name correctly and then try again
And when I tried this:
string cmd = #"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = #"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();
It works, but just opens telnet, the VBScript doesn't works.
This is the code in the .bat file:
:: Open a Telnet window
start C:\Windows\System32\telnet.exe 192.168.0.198 49211
:: Run the script
cscript SendKeys.vbs
This is the code in the .svs file:
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 500
OBJECT.SendKeys "T{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "T{ENTER}"
OBJECT.SendKeys " "
I expect the .VBS to write the command to the telnet window, however when I click the button in c# form, it just opens telnet, the VBScript doesn't works.
What you could do is find the cscript.exe file and execute it directly,like this:
for /f "tokens=*" %%C in ('where csscript.exe') do (%%C Sendkeys.vbs&goto Next)
:Next
rem This is only necessary because we want to run the script once
This migth work, but as the commenters said,using sendkeys is really not recommended,because it can cause major problems, once the windows are set incorrectly, or some of the progresses get delayed.
i am trying to run the following cmd command, that works on the CMD, from a C# console app but nothing happens:
string strCmdText = "\\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
the cmd window is emmidiatly closed after pressing F5 in VS so i can't see the output of "myTool.exe" - which does in fact print status about its progress on the cmd when run from a cmd window.
Also the desired effect of the program doesn't happen so i know it didn't work.
Need help please
Command Prompt does not take a program in as an argument to start. However, I can't see a reason to use command prompt here. You're code is starting the process "Cmd.exe" so it can start another process. Why not eliminate the middle man and just start the process you want to start? Then you can pass the process' real arguments as arguments in process.start().
Update:
You can start a program from command prompt, but it's a specific command. It goes like this:
CMD.exe /c {string of commands to execute}
So for instance, you could run it through cmd if you need to by doing this:
string strCmdText = "/c start \\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
I have to start a VPN connection (Fortinet) by code.
I have a cmd file that establish the connection.
If I call the cmd file on the shell it works pretty fine.
When I call it via Process.Start it does nothing.
It doesn't throw any exception, it seems to execute but VPN does not connect.
On the standard output I can read the echo I put on the cmd file (so it is executing the right file).
I launched a ping -d to see when the vpn goes up, when I call it via shell it goes up in a few seconds, via C# it is not.
I also tried a sleep(30000) but nothing.
My cmd (ConnectFile.cmd):
#echo off
#echo Connecting to VPN
"C:\Program Files (x86)\Fortinet\SslvpnClient\FortiSSLVPNclient.exe" connect -s "vpn myvpn"
My code (connectFile and disconnectFile are strings that contain the full path of the cmd files):
try
{
var startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = connectFile;
startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(connectFile) ?? "";
System.Diagnostics.Process process = System.Diagnostics.Process.Start(startInfo);
System.Threading.Thread.Sleep(30000);
base.GetFiles(folder);
}
finally
{
System.Diagnostics.Process.Start(disconnectFile);
}
You have to separate the parameters. FileName is for the exe file, not the whole shell command (you're explicitly saying not to use the shell). So put the parameters to the Arguments property in ProcessStartInfo.
In your case, the parameters should be:
FileName - C:\Program Files (x86)\Fortinet\SslvpnClient\FortiSSLVPNclient.exe (no quotes)
Arguments - connect -s "vpn myvpn" (again, no quoting)
Second, you have to read the standard output if you capture it. If the output buffer gets full before you read it, the called process will stop working - it has to wait for the buffer to be emptied. If you're sure the application will actually finish at some point, simply remove the Thread.Sleep and call ReadToEnd right away. Otherwise, use eg. asynchronous reading to get the data.
Also, it's usually a good idea to set WorkingDirectory. Even if the application doesn't need any data from the working directory, it's safer, since only admins can change Program Files - this helps against DLL inject hacks.
Alright, I've figured out my issue. I am using some software to remotely start programs on local computers. In doing so, I send a path across the network of a program that I want that machine to start. It uses Process.Start and stores the executing path. I then, later, resend that path and tell it close the Process that was associated with this path.
Process newProcess = Process.Start(startPath);
_runningProcesses.Add(new MCProcess(startPath, new Process);
Sometimes, I will use this to call a shortcut, which I use because I want to pass some command link arguments along with.
I've used this to call .exe and .lnk (shortcut extension) and it runs the programs just fine.
However, when passing in the path to a shortcut, the process that it returns is null! Therefor, when I send the path back to close the program, the process is null and it can't close the program.
Any solutions?
You don't need shortcut to pass arguments to the program, just do following:
Process process = new Process();
process.StartInfo.FileName = "\"C:\\my.exe\"";
process.StartInfo.Arguments = "arg1 arg2 arg3";
//...
process.Start();
In command line it would look like C:\my.exe arg1 arg2 arg3
just take the target of the shortcut if its necessary to use the shortcut.
Get target of shortcut folder
I have a simple .NET console app in C#, that runs an external process "pscp" (putty secure copy). This works great when I just run the .exe.
However, when I schedule the application in windows scheduled tasks, the application does not seem to open the external process pscp.exe. Normally it should pop up an extra console screen and open pscp.exe there. This works, just not when scheduled.
I start the process like this:
pscp.FileName = "pscp.exe";
Process p = Process.Start(pscp);
p.WaitForExit();
Any ideas on how to fix this?
Starting cmd with the /c argument runs "your exe" in a new cmd window.
The scheduled task runs under a different identity. Make sure that's working. Also, make sure you wrap the call to your exe with a cmd /c "your exe".