For some reason I can't get this batch file to run in my .NET 4.0 MVC3 project. I am using server 2008R2 64 bit - does cmd.exe operate differently?
System.Diagnostics.Process process1;
process1 = new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine = "d:\audioTemp\test.bat";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
System.Diagnostics.Process.Start("cmd.exe", #"/c d:\audioTemp\test.bat")
Your string contains a tab character \t. Either escape the backslashes:
strCmdLine = "d:\\audioTemp\\test.bat";
Or use a verbatim string literal:
strCmdLine = #"d:\audioTemp\test.bat";
Related
I need to write the serial number (text) from a textBox to cmd command using startInfo.Arguments.
The main point is, all searches I did here pointed to replace the text in the beggining or in the end of the arguments.
But I need to insert the text from textBox to the middle of the argument, like this:
string input1 = textBox1.Text;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "CMD.exe";
startInfo.Arguments = "/c adb -s "textBox1.Text" shell dumpsys battery";
Any help will be appreciated. Thank you.
The content of the textbox has already been stored in the input1 variable.
Now we have multiple options to do this in C#:
startInfo.Arguments = String.Format(#"/c adb -s ""{0}"" shell dumpsys battery", input1);
(in the # string notation, double quotes are preserved in the resulting string by doubling them)
or with concatenation:
startInfo.Arguments = "/c adb -s \"" + input1 + "\" shell dumpsys battery";
(the backslash-escaped double quotes will preserve the double quote in the resulting string)
or, recently, we can use string interpolation:
startInfo.Arguments = $#"/c adb -s ""{input1}"" shell dumpsys battery";
Either way, consider to validate the value before you execute anything you obtained from a user, especially when it is started with administrative privileges.
I have sample code to run command but its not working ( just opens CMD ) without executing the command
string strCmdLine =
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
where is problem ?
You need to add a /C
Correct syntax for CMD.exe is
CMD.EXE /c command
string strCmdLine =
"/C C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
You don't need to use cmd.exe mate...
I guess this should do the job for you...
string strCmdLine =
"\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"";
var parmaters = "google.com";
System.Diagnostics.Process.Start(strCmdLine, parmaters);
you need the parameter "/c"
string strCmdLine =
"/c C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
I have WinRAR SFX file. I know that I can extract archive using following code:
Process process = new Process();
process.StartInfo.FileName = "unrar.exe";
process.StartInfo.Arguments = "x file.rar d:\myFolder";
process.Start();
process.WaitForExit();
But how can I extract SFX file when it have known password?
Assuming your password is mypassword, you need to change your arguments line to this:
process.StartInfo.Arguments = #"x -pmypassword file.rar d:\myFolder";
Note that you shouldn't put a space after the -p before your password - or it'll prompt you.
I also added a # to mark the string as a literal, otherwise it'll try to treat the \m in the file name as an escape character.
you can use -p as parameter
Assuming your password is 123456
Process process = new Process();
process.StartInfo.FileName = "unrar.exe";
process.StartInfo.Arguments = "x -p123456 file.rar d:\myFolder";
process.Start();
process.WaitForExit();
I am trying to use a cmd command prompt that works just fine when I use it in a normal cmd window via Windows --> Start --> cmd
My code looks like this
"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat" -f
"C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp"
That works just fine.
The problem is when I am trying to write this code in C# by opening a cmd window via C#
This is the c# code
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c \"C:\\Program Files\\Rapid-I\\RapidMiner5\\scripts\\rapidminer.bat\" -f \"C:\\Users\\user\\.RapidMiner5\\repositories\\Local Repository\\test.rmp\"");
You can see I quoted the same way like the code that work in the regular cmd
But I am getting a can't recognize program files.
Try this:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
p.Arguments = #"/c -f ""C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp""";
Process.Start(p);
I modified my original code to add arguments to your process.
One last thing you could try is putting your original command into a batch file and calling the batch file from your ProcessStartInfo object like so:
C#:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\run.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
Process.Start(p);
I have this method
private void _executeCommand(string commandStr, int timeout)
{
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + commandStr);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
Thread.Sleep(timeout);
}
catch (ExecutionEngineException e)
{
throw e;
}}
somehow, if I pass a string called myCmd, _executeCommand(myCmd, timeout), it does nothing. But if I pass the exact string value of myCmd, _executeCommand("copy //data//file \"C://Program Files/myApp\"", timeout), it was able to execute. Could anyone see what the problem is?
If you have a command that has its own arguments you want to execute using /c, you will need to enclose the entire command and its argument after /c in quotes. Further, if you're using a path that contains spaces as one of the arguments to your command you'll have to quote that too, i.e #"/c ""copy ""C:\My Folder\Ny File.txt"" ""C:\My Other Folder"""""
Also, If you have multiple commands that you want it to perform you will either have to put the commands in a batch file and execute that or enclose them in quotes and separate them with &&, i.e. #"/c ""cd \ && dir""".
Note that the doubling of quotes in my examples is how you escape quotes when using a string literal in C#. The # preceding the string tells the compiler to take the string literally and not to interpret \ characters as special.
Taking a look at my answer to Sending commands to cmd prompt in C# is probably a good idea too as it explains a lot about how this stuff works.