执行命令 C# 不起作用
我有一个程序,我想执行以下操作:
- 获取文件(类型未知)
- 在本地保存文件
- 在文件上执行一些命令(未知)
我已经处理了步骤 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:
- Get a file (type unknown)
- Save file locally
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此处:
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.我认为问题是您的
Process
设置不正确。您当前的代码将使用默认的 .txt 文件打开器打开 .txt 文件(因为您指定了 procStart.UseShellExecute = true;),然后您设置 procStart.Arguments = "mkdir TestDir";< /code> 但这实际上不会对您有帮助,因为所发生的只是
"mkdir TestDir"
将作为命令行参数传递给notepad.exe
。您真正想要的是:
ProcessStartInfo
,其中FileName
设置为cmd.exe
(并设置Arguments = "/C mkdir 测试”
)CreateDirectory()
直接方法。我更喜欢#2,因为它更清楚地显示你想要做什么,但两者都应该有效。
更新:如果您需要使用选项 1,那么您应该使用以下代码来查看出了什么问题:
其他一些注意事项:
.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 setprocStart.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 tonotepad.exe
.What you really want is either:
ProcessStartInfo
with theFileName
set tocmd.exe
(and setArguments = "/C mkdir Test"
)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:
A couple other notes:
.ashx
handler then a web page; page loads should be as snappy as possible.