Unable to run .exe application using C# code
I have an exe that I need to call from my C# Program with two arguments (PracticeId, ClaimId)
For example:
Suppose I have an application test.exe
, whose functionality is to make a claim according to the given two arguments.
On cmd I would normally give the following command as:
test.exe 1 2
And it works fine and performs its job of conversion.
But I want to execute the same thing using my c# code. I am using the 开发者_如何学Cfollowing sample code:
Process compiler = new Process();
compiler.StartInfo.FileName = "test.exe" ;
compiler.StartInfo.Arguments = "1 2" ;
compiler.StartInfo.UseShellExecute = true;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
When I try to invoke test.exe
using the above code, it fails to perform its operation of making a claim txt file.
Where is the issue in this? Whether the problem is threading or not, I don't know.
Can someone please tell me what I need to change in the above code?
The code you've supplied fails with the following error when I run it:
System.InvalidOperationException
The Process object must have the UseShellExecute property set to false in order to redirect IO streams.
The following code executes correctly and passes the arguments through:
var compiler = new Process();
compiler.StartInfo.FileName = "test.exe";
compiler.StartInfo.Arguments = "1 2";
compiler.StartInfo.UseShellExecute = true;
compiler.StartInfo.RedirectStandardOutput = false;
compiler.Start();
although I haven't tested my code yet but I hope this will help you to do what you want to do ...
Process.Start("xyz.exe", "1 2");
Let me know whether it works or not.
Check what is your working directory. Is test.exe in your path? If not, you will need to supply the path. It is good practice to supply the path if you know where it is. You can dynamically construct that from Application path, assembly executing path, or some user preference setting.
MSDN should help. They look like they've updated the site some, and the example for your particular question is very detailed.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
精彩评论