4

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 following 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?

1
  • Are you sure "test.exe" is even being executed? If I had to bet, I'd bet it is not being executed at all. Commented Mar 31, 2010 at 4:37

4 Answers 4

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

the msg says "The Process object must have the UseShellExecute property set to false in order to redirect IO streams" but your code uses true?
Correct, so I also disabled redirection by setting RedirectStandardOutput = false. That way it is happy
0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.