3

I have a program where I want to do the following:

  1. Get a file (type unknown)
  2. Save file locally
  3. Execute some command (unknown) on the file

I have step 1 and 2 taken care of but I am struggling on step 3. I use the following code:

NOTE: The file type and command are just used for testing

//Redirects output
procStart.RedirectStandardOutput = false;
procStart.UseShellExecute = true;
procStart.FileName = "C:\\Users\\Me\\Desktop\\Test\\System_Instructions.txt";
procStart.Arguments = "mkdir TestDir";

//No black window
procStart.CreateNoWindow = true;

Process.Start(procStart);

The .txt document will open, but the command will not be run (there will be no testDir in the test folder)

Suggestions?

5
  • Where are you getting that error from? This is definitely an exception originating from a compiled line of code, not a statement entered in the immediate window? Commented Aug 10, 2011 at 14:11
  • I'm with Kragen on this one: that error usually means you've viewed something in the locals or watch window, or inspected an expression via the immediate window. I assume you're doing that because the code is otherwise not working, but I don't think that error message is your actual problem. Commented Aug 10, 2011 at 14:15
  • @Keragan - I get the error on the Response.End() line when I run the program in my debugger, I removed the line like @Davide suggested I don't get any errors, but the directory is not created Commented Aug 10, 2011 at 14:19
  • What kind of program is this? (Response.End() suggests ASP.NET, but I'd prefer not to assume.) Commented Aug 10, 2011 at 14:22
  • @dlev - It is a asp.net program. The method is being run on pageload Commented Aug 10, 2011 at 14:24

2 Answers 2

3

see here:

https://stackoverflow.com/search?q=+optimized+or+a+native+frame+is+on+top+of+the+call+stack

you should not call Response.End like that because this terminates it.

Sign up to request clarification or add additional context in comments.

Comments

2

I think the issue is that your Process isn't setup properly.

Your current code will open a .txt file using the default .txt file opener (since you specified procStart.UseShellExecute = true;) You then set procStart.Arguments = "mkdir TestDir"; But that's not actually going to help you, since all that will happen is "mkdir TestDir" will be passed as command-line arguments to notepad.exe.

What you really want is either:

  1. A separate ProcessStartInfo with the FileName set to cmd.exe (and set Arguments = "/C mkdir Test")
  2. Use the CreateDirectory() method directly.

I would prefer #2, since it more clearly shows what you'd like to do, but either should work.

UPDATE: If you need to use option 1, then you should use the following code to see what's going wrong:

Process userCommandProc = Process.Start(procStart);
userCommandProc.WaitForExit();

if (userCommandProc.ExitCode != 0)
{
    // Something has (very likely) gone wrong
}
else
{
    // Most likely working
}

A couple other notes:

  1. This process will be run on the server, not the client computer. If you need the latter, you're out of luck if you want to use a web app.
  2. This kind of processing is probably better left to a standard .ashx handler then a web page; page loads should be as snappy as possible.

7 Comments

I am trying to do the first thing that you mentioned (since I will not know what the command is going to be. I have edited my code to the following: ProcessStartInfo procStart = new ProcessStartInfo(); procStart.RedirectStandardOutput = false; procStart.FileName = "cmd.exe"; procStart.Arguments = "/C mkdir TestDir"; procStart.CreateNoWindow = true; Process.Start(procStart); but the directory is still not created
Thanks for the help. I ran the code you have added and I got an ExitCode of '1' so there is something wrong with the code. I am going to have to do some more research to see what is going wrong
I keep getting an System.InvalidOperatinException. Any idea why? I switched the code over to my .ashx class also.
@Peppered when do you get that exception?
When I run the debugger and watch userCommandProc after the userCommandProc.WaitForExit(); line. The exception is thrown by userCommandProc
|

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.