I am trying to make a directory using this code to see if the code is executing but for some reason it executes with no error but the directory is never made. Is there and error in my code somewhere?
var startInfo = new
var startinfo = new ProcessStartInfo();
startinfo.WorkingDirectory = "/home";
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
Console.WriteLine ("Shell has been executed!");
Console.ReadLine();
This works best for me because now I do not have to worry about escaping quotes etc...
using System;
using System.Diagnostics;
class HelloWorld
{
static void Main()
{
// lets say we want to run this command:
// t=$(echo 'this is a test'); echo "$t" | grep -o 'is a'
var output = ExecuteBashCommand("t=$(echo 'this is a test'); echo \"$t\" | grep -o 'is a'");
// output the result
Console.WriteLine(output);
}
static string ExecuteBashCommand(string command)
{
// according to: https://stackoverflow.com/a/15262019/637142
// thans to this we will pass everything as one command
command = command.Replace("\"","\"\"");
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "-c \""+ command + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit();
return proc.StandardOutput.ReadToEnd();
}
}
This works for me:
Process.Start("/bin/bash", "-c \"echo 'Hello World!'\"");
My guess is that your working directory is not where you expect it to be.
See here for more information on the working directory of Process.Start()
also your command seems wrong, use && to execute multiple commands:
proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";
Thirdly you are setting your working directory wrongly:
proc.StartInfo.WorkingDirectory = "/home";
Related
as described in the title, I wanna open up the ubuntu-shell on my windows pc, passing a
"cd /mnt/c/users/xyz/desktop" to it then passing a
"python3 some_script.py arg1, arg2" to it
all this works wonderful if done manually via mouseclicks but from code (see below:)
it doesnt write anything to the console which opens.
string ExecuteCommand(string command)
{
// Execute wsl command:
var StartInfo = new ProcessStartInfo
{
FileName = #"bash.exe",
WorkingDirectory = #"C:\Windows\System32",
//Arguments = "/c " + "root#DESKTOP-OUTEVME:~#",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Normal,
};
using (var process = Process.Start(StartInfo))
{
process.StandardInput.WriteLine();
process.EnableRaisingEvents = true;
process.OutputDataReceived += (s, e) => MessageBox.Show(e.Data);
process.BeginOutputReadLine();
process.StandardInput.WriteLine(command);
process.StandardInput.WriteLine("exit");
process.WaitForExit();
//result = process.StandardOutput.ReadToEndAsync().Result;
}
return result;
}
return ExecuteCommand(#"wsl cd /mnt/c/users/shho3/desktop");
Anyone maybe an idea what I could do wrong?
Much thanks!
You could pass everything in the command line instead of piping into the process. I think it would save you a lot of trouble. Try bash.exe -c "cd /mnt/c/Users/shho3/Desktop; python some_script.py arg1 arg2":
Process.Start("bash.exe", "-c \"cd /mnt/c/Users/shho3/Desktop; python some_script.py arg1 arg2\"").WaitForExit()
Alternatively you can also just set the working directory to C:\Users\shho3\Desktop (instead of C:\Windows\System32) and call bash.exe -c "python some_script.py arg1 arg2", then you don't even have to convert the path:
Process.Start(new ProcessStartInfo("bash.exe", "-c \"python some_script.py arg1 arg2\"") {
WorkingDirectory = "C:\\Users\\ssho3\\Desktop"
}).WaitForExit()
What is wrong with this code to run a command in command prompt? I try to run this code and it does not give any error and it does not do what it is supposed to do. It works fine if I copy the command to command prompt and run it manually?
Thank you!
[TestMethod]
public void TestProcess()
{
string command1 = #"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";
ProcessStartInfo processInfo;
Process process;
//I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.
processInfo = new ProcessStartInfo("cmd.exe", #"C:\sejda-console-3.2.83\bin " + command1);
processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
Alternatively, I am trying this one too which does not work either.
[TestMethod]
public void TestProcess3()
{
string MyBatchFile = #"C:\sejda-console-3.2.83\bin\sejda-console.bat";
string _sourcePath = #"C:\TestFiles\test.pdf";
string _targetPath = #"C:\TestFiles\split1\";
var process = new Process
{
StartInfo = {
Arguments = String.Format("/C simplesplit --files -f {0} -o {1} -s all", _sourcePath, _targetPath)
}
};
process.StartInfo.FileName = MyBatchFile;
bool b = process.Start();
}
Try this processInfo:
var batch = "sejda-console.bat";
var sourcePath = #"C:\TestFiles\test.pdf";
var targetPath = #"C:\TestFiles\split1\";
var processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = #"C:\sejda-console-3.2.83\bin";
processInfo.FileName = "cmd.exe";
processInfo.Arguments = $"/C {batch} simplesplit --files -f \"{sourcePath}\" -o \"{targetPath}\" -s all";
// todo set windows style etc
Also have a look at Executing Batch File in C# for error handling.
You are missing /C to send arguments to cmd.exe
Add backslash after \bin\
Wrap your command line arguments with quotes.
So your code should look like:
[TestMethod]
public void TestProcess()
{
string command1 = #"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";
ProcessStartInfo processInfo;
Process process;
//I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.
processInfo = new ProcessStartInfo("cmd.exe", #"/C \"C:\sejda-console-3.2.83\bin\" + command1 + "\"");
processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
I'm trying to open run a few arguments using cmd.exe from ProcessStartInfo in C#
but my folder navigation needs to include double quotes eg. "C:\this is\my\folder site"
as you see the reason for using double quotes is because the folders have space on their name.
this is my code
var ddd = "\"" + projectPath + "\"";
var strCmdTxt = "/c cd " + ddd + " && code .";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = strCmdTxt, UseShellExecute = true, CreateNoWindow= true
};
process.StartInfo = startInfo;
process.Start();
BUT, what it runs is something like this
cd\ "C:\this is\my\folder site\"
which, just returns me to C drive
The command should be cd "C:\this is\my\folder site"
Looks like what you're trying to achieve is start VS Code in the specified folder. Consider using the working directory of the process you're starting, instead of trying to navigate to that directory and starting VS Code in there. Here is a method to help with that:
private static void StartVSCodeInFolder(string projectPath)
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "C:/Program Files/Microsoft VS Code/Code.exe",
Arguments = ".",
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = projectPath
};
process.StartInfo = startInfo;
process.Start();
}
}
Hope this helps.
Could you not change the working directory using the Environment class and simply using "code.exe".
It seems like it would be a cleaner approach.
Environment.CurrentDirectory = #"C:\Program Files\Microsoft VS Code";
This question already has answers here:
How To: Execute command line in C#, get STD OUT results
(18 answers)
Closed 7 years ago.
I am trying to run a command line script from C#. I want it to run without a shell and place the output into my string output. It doesn't like the p.StartInfo line. What am I doing wrong? I am not running a file like p.StartInfo.FileName = "YOURBATCHFILE.bat" like How To: Execute command line in C#, get STD OUT results. I need to set the "CMD.exe" and command line string. I have tried p.Start("CMD.exe", strCmdText); but that gives me the error: "Memer 'System.Diagnostics.Process.Start(string,string)' cannot be accessed with an instance reference; qualify it with a type name instead."
string ipAddress;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string strCmdText;
strCmdText = "tracert -d " + ipAdress;
p.StartInfo("CMD.exe", strCmdText);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
This code gives me the correct ouput.
const string ipAddress = "127.0.0.1";
Process process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = "/C tracert -d " + ipAddress
}
};
process.Start();
process.WaitForExit();
if(process.HasExited)
{
string output = process.StandardOutput.ReadToEnd();
}
You are using StartInfo incorrectly. Have a look at documentation for ProcessStartInfo Class and Process.Start Method (). Your code should look something like this:
string ipAddress;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string strCmdText;
strCmdText = "/C tracert -d " + ipAdress;
// Correct way to launch a process with arguments
p.StartInfo.FileName="CMD.exe";
p.StartInfo.Arguments=strCmdText;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Also, note that I added /C argument to strCmdText. As per cmd /? help:
/C Carries out the command specified by string and then terminates.
I want to run this:
string command = "echo test > test.txt";
System.Diagnostics.Process.Start("cmd.exe", command);
It's not working, what am I doing wrong?
You are missing to pass the /C switch to cmd.exe to indicate that you want to execute a command. Also notice that the command is put in double quotes:
string command = "/C \"echo test > test.txt\"";
System.Diagnostics.Process.Start("cmd.exe", command).WaitForExit();
And if you don't want to see the shell window you could use the following:
string command = "/C \"echo test > test.txt\"";
var psi = new ProcessStartInfo("cmd.exe")
{
Arguments = command,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(psi))
{
process.WaitForExit();
}
This should sort of get you started:
//create your command
string cmd = string.Format(#"/c echo Hello World > mydata.txt");
//prepare how you want to execute cmd.exe
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.Arguments = cmd;//<<pass in your command
//this will make echo's and any outputs accessiblen on the output stream
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process p = Process.Start(psi);
//read the output our command generated
string result = p.StandardOutput.ReadToEnd();