I am trying to execute a cmd command from .NET and using the find cmd command as a pipe. But the find cmd command takes the value in string but when i am creating my string variable instead of taking double quotes it is replacing it with /"".
My string variable is "/c sc QUERY ServiceName | find \"START\"" and instead of passing "/c sc QUERY ServiceName | find "START"" this to cmd shell it is passing "/c sc QUERY ServiceName | find \"START\"" and my find command is failing.
Below is my .NET code
string cmdstr = "/c sc QUERY ServiceName | find \"START\"";
Process prc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = cmdstr;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
prc.StartInfo = startInfo;
prc.Start();
string result;
result = prc.StandardOutput.ReadToEnd();
prc.WaitForExit(10);
When i debug the code the cmdstr value does not contain double quotes but it contains the \"
Regarding your code:
It's fine and equivalent to manually open up cmd.exe and type sc QUERY ServiceName | find "START"
If i change ServiceName to W32Time and START to RUNNING, then I even get a non empty result (on my machine).
Regarding the debugger:
The debugger shows you the not evaluated version of the string (i.e. that what you actually type in the code).
If you want to see the string with evaluated escape-sequences, than you must use the "Text-Quickview"-Dialog (I don't know the exact title in English):
string cmdstr = string.Empty;
cmdstr = "/c sc QUERY ServiceName | find \"START\"";
cmdstr = cmdstr.Replace("\"", "");
cmdstr ="/c sc QUERY ServiceName | find START";
-Hope it works .
Related
I am trying to run a command in cmd and read the result, where the result involves some Swedish and Finnish character by the below mentioned code. Now the code results ok when it is run on physical machine
possibly this StandardOutputCoding logic handle those character successfully. However when I am running it on an Citrix machine, it is not working. Even querying Textinfo.OEMPageCode does not show anything where as in physical machine it returns int value. I believe this piece of code (Textinfo.OEMPageCode) is not working in citrix that's why all those special character do not show properly. Can anyone please help how to solve this. Thanks in advance.
string userName = environment.username;
string fullName = "\"Full Name\"";
command = String.Format("net user {0} /domain | findstr /C:{1}", userName, fullName);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
procStartInfo.StandardOutputEncoding = Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
proc.StartInfo = procStartInfo;
proc.Start();
userFullName = proc.StandardOutput.ReadToEnd();
userFullName = userFullName.Replace("Full Name", "").Trim();
}
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'm trying to run CMD from my code. This is the line that I run in my command line, and it works when I run it manually:
C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
This is what I have in my code:
string commandText = String.Format("/C {0}{1} & node csvToJson.js", root, csvToJsonFolder);
Process.Start("CMD.exe", commandText);
commandText evaluates to /C C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
It runs without error, but nothing seems to have happened. The command prompt doesn't open, so I can't see any errors that may have occurred. The command is supposed to result in a file being written to a particular folder, and when I run the command manually the file gets written, but when I run my code the file does not get written.
EDIT: I changed my code to this:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = commandText;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
string result = process.StandardOutput.ReadToEnd();
The result is just an empty string. There is no error message or anything.
The problem was in my command text, I forgot "cd". Should have been "/C cd C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js"
I have a program that needed running from the cmd with arguments, say the execute file called
program.exe
And i need to run it from the cmd with args, the whole command in the cmd look like this:
c:\ram\program.exe /path = c:\program files\NV
As you can see the path is : "c:\ram\"
The execute file is : "program.exe"
The args that i need to send is : /path = c:\program files\NV
How can i do it ?
I try to open process like this :
string strArguments = #"/path = c:\program files\NV";
Process p = new Process();
p.StartInfo.FileName = "program.exe";
p.StartInfo.WorkingDirectory = "c:\\ram\\";
p.StartInfo.Arguments = strArguments;
p.Start();
And its not good, i figure that the problem could be that i'm not accessing the exe file from the CMD, maybe i'm wrong...any body got idea how can i do it ?
Thanks
Try these things
p.StartInfo.FileName = "c:\\ram\\program.exe"; without setting Working Directory
and this one is more likely the source of the problem
string strArguments = #"/path = ""c:\program files\NV""";
When there is a space in a path, you have to enclose the whole path in quotation marks. The complete code is as follows
string strArguments = #"/path=""c:\program files\NV""";
Process p = new Process();
p.StartInfo.FileName = #"c:\ram\program.exe";
p.StartInfo.Arguments = strArguments;
p.Start();
It should do exactly what are you trying to do.
1.run "cmd.exe".
2.go to this dir: "c:\ram\" (in the cmd of course).
3.execute the file "program.exe" with this argument: "/path = c\:program files\NV"
It takes the program.exe in the c:\ram\ folder and executes it using cmd with the specified arguments.
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);