开发者

Executing command C# Not Working

开发者 https://www.devze.com 2023-03-27 06:11 出处:网络
I have a program where I want to do the following: Get a file (type unknown) Save file locally Execute some command (unknown) on the file

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 th开发者_如何学Pythone 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?


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消