I'm trying to start a local instance of notepad with a text file to try out c# cmd line arguments for eventual use in a remote connection script. I'm using System.Diagnostics.Process, but the StartInfo.Arguments doesn't actually run completely and open the notepad instance.
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"cd\ start notepad C:\test\testcmdline.txt";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
The window opens at root, which tells me the cd\ is working, but the "start notepad" does not seem to be running.
Am I missing something about the structure of StartInfo.Arguments?
EDIT: I'm trying to figure out how to run a python script on a remote server, and using this as a test for running things in cmd in c#. While it's fine to run this in notepad, I'm not sure if the principle would carry over to the eventual implementation of running a python script remotely so I'm attempting to learn how to run items through cmd in C# in general.
I ended up using the more simple 2 arg Process.Start.
string cmdText;
cmdText = #"/C C:\test\testcmdline.txt";
Process.Start("cmd.exe", cmdText);
Try adding /c in the beginning of the Arguments.
Or the above task can be done as below
var process = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = #"/c start notepad C:\test\testcmdline.txt"
};
Process.Start(process );
Related
I'm trying to run a curl command from a C# program. My code is below. When I run the code below, I get an exception that the file is not found. I want to be able to do this but I do not want to use a batch file as a parameter for the filename. That is because the arguments for my curl command are variable based upon other conditions in the C# code. My variable strCmdText has the arguments for the curl command (the source and destination files). There are other examples of this on Stackoverflow, but they all use a batch file which I'm trying to avoid.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";
p.StartInfo.Arguments = strCmdText;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
I changed my code to the following:
System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo();
p.UseShellExecute = true;
p.WorkingDirectory = "C:\\Windows\\System32\\";
p.FileName = "curl.exe";
p.ErrorDialog = true;
p.CreateNoWindow = true;
System.Diagnostics.Process.Start(p);
From a DOS prompt, curl does exist in this directory. But I still get the curl not found message.
Something has to be strange with the path here. When I put a break point in though, and view the Environment class, System32 is in the path.
Curl is available at the location: C:\Windows\System32\curl.exe
That only leaves the source file to be the culprit of a "File not found" issue.
As you're launching curl through a process, ensure that your paths are escaped properly in your startup arguments.
Alternatively, you could launch curl through cmd (through a process), you can try with the following, changing the command-line arguments from --help to suit your desired action.
string script = $"\"C:\\Windows\\System32\\curl.exe\" --help";
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd",
Arguments = script
}
};
process.Start();
Please note that this is in principle, essentially using a batch file as it's just throwing some commands into a cmd.
I had the exact same problem. Just delete curl.exe from System32 and place it on another folder (dont't forget the dependences, dlls, etc.).
Then in the line
p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";
Overwrite "C:\\Windows\\System32\\curl.exe" to "C:\\NEW PATH\\curl.exe".
Note: You MUST delete it from System32. If you just copy to the new location it will still don't work.
I'm writing a a small WPF app that will help me run JMeter tests in non-GUI mode without the hassle of typing JMeter commands and file paths into the console every time I want to run a test. This means that my WPF app needs to open up CMD in location where my JMeter is intalled and then pass an argument (command line).
This is how I open up CMD with a specific path and arguments I pass:
private void RunScript()
{
var process = new Process();
var startInfo = new ProcessStartInfo
{
WorkingDirectory = "#D:\\Programi\\apache-jmeter-5.1\\bin",
WindowStyle = ProcessWindowStyle.Normal,
FileName = "cmd.exe",
Arguments = "/K jmeter -n -t " + scriptDirectoryPath
};
process.StartInfo = startInfo;
process.Start();
}
As you can see, the path where the CMD needs to open is "D:\Projekti\JMeteor\JMeteorApp\JMeteorApp\bin", but the path in CMD is "D:\Projekti\JMeteor\JMeteorApp\JMeteorApp\bin\Debug>"
How do I remove the "Debug" portion in CMD path? I tried switching solution configuration to "Release" but that just replaces "Debug" with "Release" in path.
Do not write the # inside the string
use either
WorkingDirectory = "D:\\Programi\\apache-jmeter-5.1\\bin"
or (I guess you wanted to use the # for a verbatim string)
WorkingDirectory = #"D:\Programi\apache-jmeter-5.1\bin"
I have a button in my form that when its clicked, it will open command prompt and automatically run a javascript file. My code so far only opens the command prompt. How do you run a javascript file?
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.Start();
Now that we know that you're trying to launch a Node.js app, try this new version.
And don't forget that C# is pretty well documented on MSDN.
If the rest of your code is working as is, and you don't actually want/need to run CMD.exe, this should do the trick:
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\nodejs\node.exe";
process.StartInfo.Arguments = #"P:\ath\To\Your\File.js";
process.Start();
As a holdover from the original question: If you had been trying to launch a JScript script, you would want to use #"C:\Windows\System32\Cscript.exe", or if you want to launch a JScript script without seeing a command prompt window, replace Cscript with Wscript.
Try this:
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("Cscript.exe \"PathToYourFile\\file.js\"");
//OR
//process.StandardInput.WriteLine("Wscript.exe \"PathToYourFile\\file.js\"");
process.StandardInput.Flush();
process.StandardInput.Close();
System.Diagnostics.Process process = new System.Diagnostics.Process ();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo ();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "md " + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
process.StartInfo = startInfo;
process.Start ();
I'm attempting to make a directory on the desktop with this command, it doesn't make one however. Can anyone tell me why?
Just do this:
Directory.CreateDirectory(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"my new folder name"));
Always prefer using the .NET class library instead of invoking external processes to do your work, unless you have a very specific reason not to do so.
One of the reasons your code is not working is because you are using the wrong syntax for cmd.exe. In order to pass a command as an argument, you have to use the following with the /K switch (use cmd /? for more information):
cmd.exe /K MD "c:\test\blah"
Another reason your code won't work is that the path you're providing to the MD command is just the path to the desktop itself:
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
You have forgotten to append the name of the folder you want to create on the desktop.
I'm working on making a tech-toolkit program, and included in this 'toolkit' will be a button which runs a defrag on the local disk. Currently the batch file I've made for this is simple, it just runs a basic fragmentation analysis:
defrag C: /A
The C# code behind the button that triggers this is:
System.Diagnostics.ProcessStartInfo procInfo =
new System.Diagnostics.ProcessStartInfo();
procInfo.Verb = "runas";
procInfo.FileName = "(My Disk):\\PreDefrag.bat";
System.Diagnostics.Process.Start(procInfo);
This code does exactly what I want, it calls UAC then launches my batch file with Administative Privledges. Though once the batch file is ran, the output I recieve to the command console is:
C:\Windows\system32>defrag C: /A
'defrag' is not recognized as an internal or external command,
operable program or batch file.
What causes this Error and how do I fix it?
Check to make sure your defrag.exe file actually exists in C:\Windows\System32.
Try fully qualifying the 'defrag' command as:
C:\WINDOWS\system32\defrag.exe C: /A
Open up a cmd prompt and run this command: defrag.exe /? and post in the question what you get.
First of all: set yout project Platform Target property to Any CPU and untick the Prefer 32-bit option (99,9% this is the issue). Then... why starting a batch that invokes the command when you can just do this?
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = "/C defrag C: /A";
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Works like a charm on my machine. For multiple commands:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
Process cmd = Process.Start(info);
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(command1);
sw.WriteLine(command2);
// ...