执行命令 C# 不起作用

发布于 2024-11-29 00:50:44 字数 602 浏览 1 评论 0原文

我有一个程序,我想执行以下操作:

  1. 获取文件(类型未知)
  2. 在本地保存文件
  3. 在文件上执行一些命令(未知)

我已经处理了步骤 1 和 2,但我在步骤 3 上遇到了困难。我使用以下代码:

注意: 文件类型和命令仅用于测试

//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);

.txt 文档将打开,但命令不会运行(不会有 <代码>testDir在test 文件夹)

有建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-12-06 00:50:44

请参阅此处:

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

你不应该调用 Response.End就像那样,因为这会终止它。

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.

烟若柳尘 2024-12-06 00:50:44

我认为问题是您的 Process 设置不正确。

您当前的代码将使用默认的 .txt 文件打开器打开 .txt 文件(因为您指定了 procStart.UseShellExecute = true;),然后您设置 procStart.Arguments = "mkdir TestDir";< /code> 但这实际上不会对您有帮助,因为所发生的只是 "mkdir TestDir" 将作为命令行参数传递给 notepad.exe

您真正想要的是:

  1. 一个单独的 ProcessStartInfo ,其中 FileName 设置为 cmd.exe (并设置 Arguments = "/C mkdir 测试”
  2. 使用 CreateDirectory() 直接方法。

我更喜欢#2,因为它更清楚地显示你想要做什么,但两者都应该有效。

更新:如果您需要使用选项 1,那么您应该使用以下代码来查看出了什么问题:

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

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

其他一些注意事项:

  1. 此过程将在服务器上运行,而不是在客户端计算机上运行。如果您需要后者,那么如果您想使用网络应用程序,那么您就不走运了。
  2. 这种处理可能最好留给标准的 .ashx 处理程序,而不是网页;页面加载应该尽可能快。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文