Run exe with arguments from c# - c#

I have referred many articles before posting this question. In my case, my exe "abc" need to pass the file name as "--run" parameter.
If I call this code from windows run window
c://path/abc.exe --run filename.json
It works but if I try to run from ProcessStart using
Process p = Process.Start(new ProcessStartInfo(#"c://path/abc.exe")
{
Arguments = "--run filename.json",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true
});
It's not working. It just opens a window for a sec and then closes it and in the background also nothing happened.

Where is your filename.json located?
Have you tried setting up ProcessStartInfo.WorkingDirectory?

Can you please try passing the arguments with double quote, i.e :
Process p = Process.Start(new ProcessStartInfo(#"C://Users/source/repos/ConsoleApp5/bin folder/ConsoleApp5.exe")
{
Arguments = "\"--run filename.json\"",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true
});
Looking at github for Process.cs : here , it is calling CreateProcessWithLogonW function which seems to use space as delimiter to pass arguments. I tried with a simple console app, and I am able to get the output as follows:
--run filename.json

Related

Process started by Process.Start does not terminate when stdout/stderr redirection is enabled

I'm starting a process with Process.Start:
var psi = new ProcessStartInfo("yt-dlp.exe"){
ErrorDialog = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
ArgumentList = {
"--prefer-insecure",
"--no-check-certificates",
"--dump-json",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ"
},
};
var process = Process.Start(psi);
process.EnableRaisingEvents = true;
process.Exited += HandleProcessExit;
HandleProcessExit is never called - the process never actually exits, judging by the fact that the process does not disappear from the Task Manager. Calling the process itself outside of C# (yt-dlp.exe --prefer-insecure --no-check-certificates --dump-json https://www.youtube.com/watch?v=dQw4w9WgXcQ) in my command terminal works fine - the process exits.
Changing RedirectStandardError and RedirectStandardOutput to false seems to fix the issue... however, I need to access these streams.
For context, the executable I'm using is yt-dlp, but I don't think that is the issue. How can I fix this?

Why cant I start a process without using ShellExecute set to true?

So I'm currently trying to start the Command Prompt window from my ASP.NET Core MVC WebApp which is running in Debug mode when I click a button but for some reason it won't start unless I specify UseShellExecute and set it to True.
This is a problem since if I do set it to True I can't redirect standard input which is a bad thing.
I need to be able to do that.
Here is the code
public static void Start(string dirPath)
{
Process serverProcess = new Process
{
StartInfo =
{
CreateNoWindow = false,
FileName = "cmd.exe",
Arguments = "",
WindowStyle = ProcessWindowStyle.Maximized,
RedirectStandardInput = true
}
};
serverProcess.Start()
}
It does invoke the code, because keep in mind, it works when I do UseShellExecute = True and it also hits the breakpoints etc.
How do I start the cmd or a specific batch file without setting UseShellExecute = True ?
UseShellExecute is false by default for Core . If you don't want to set UseShellExecute . Try setting Arguments below as workaround :
Arguments = $"/c start cmd.exe",
Reference : https://github.com/dotnet/corefx/issues/21767

Python script execution returns null to textblock in wpf

I've looked through lots of questions about this, but I still can't figure out what's going on in my code. I have a simple form, with a browse button so you can pick a script to run. You then press run, and the output of the script populates into a textblock.
Right now, I'm running a python script that is simply:print "Hello World".
Here's my code to pick up the output:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "python.exe",
Arguments = script,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
ErrorDialog = true,
CreateNoWindow = true //no black window
}
};
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
test.Text = output; //This is the textblock
What happens is that the textbox content changes to blank. No output is showing, just blank. This is pretty straightforward and super frustrating that I can't figure this out. Why is it returning null?
You are starting a new Process to get the output, but there can be a delay in the process to fetch output. Thus the string output is null and is stored in the TextBox. Make sure that output is printed only when the process is completed.

Command Line Process

So after scouring the web I found a few articles (some on stackoverflow) which described how to execute a command line prompt by starting a new process in c#. The second argument, which I've commented out, works just fine, but the one I actually need (the first one) doesn't. It returns the error "Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser" When I open up a command line (non-programatically) and then execute the same command (aside from the escaped quotations) it works great. Any idea's about what the problem could be? Thanks!
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "CMD.exe",
Arguments = "/c java -mx100m -cp \"*\" edu.stanford.nlp.parser.lexparser.LexicalizedParser edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz libtest.txt",
// Arguments = "/c echo Foo",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.WriteLine(proc.StandardError.ReadToEnd());
Ensure that the executing path where you start your process is correct!
You can use Process Monitor from SysInternals to figure out where that class is looked for.

Exec imagemagick convert via C#

When I open a command window in windows and use the imagemagick convert command:
convert /somedir/Garden.jpg /somedir/Garden.png
It works as expected.
What I am trying to do is executing the same command as above using C#.
I tried using System.Diagnostics.Process, however, no foo.png gets created.
I am using this code:
var proc = new Process
{
StartInfo =
{
Arguments = string.Format("{0}Garden.jpg {1}Garden.png",
TEST_FILE_DIR,
TEST_FILE_DIR),
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = false,
RedirectStandardError = false
}
};
proc.Start();
No exception gets thrown, but no .png is written, either.
My guess is that TEST_FILE_DIR contains a space - so you have to quote it.
Try this:
Arguments = string.Format("\"{0}Garden.jpg\" \"{1}Garden.png\"",
TEST_FILE_DIR,
TEST_FILE_DIR)
You might also want to give the filename including the extension, e.g.
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert.exe"

Categories

Resources