C# ProcessStartInfo 问题
我正在使用 ProcessStartInfo 来修补具有如下文本文件的文件(通过 cmd.exe):
app.exe temp.txt patch.ips
我编写了以下代码:
ProcessStartInfo P = new ProcessStartInfo("app.exe");
P.Arguments = "temp.txt " + _patchpath;
P.CreateNoWindow = true;
P.UseShellExecute = false;
P.RedirectStandardOutput = true;
Process.Start(P);
app.exe 和 temp.txt 相对于我的应用程序路径(注意:app.exe 不是我的 C# 应用程序的名称,它只是我用于进程的程序),但 _patchpath 是绝对路径,如 D:\blah\file.ips。问题是,如果该过程是绝对的,则该过程不起作用(_patchpath 应该使用文件 temp.txt 进行修补),但如果其相对于我的应用程序目录,则该过程确实有效。这是为什么?我该如何解决它?
如果我需要澄清,请告诉我。
I am using a ProcessStartInfo to patch a file with a text file like this (through cmd.exe):
app.exe temp.txt patch.ips
I wrote this code:
ProcessStartInfo P = new ProcessStartInfo("app.exe");
P.Arguments = "temp.txt " + _patchpath;
P.CreateNoWindow = true;
P.UseShellExecute = false;
P.RedirectStandardOutput = true;
Process.Start(P);
app.exe and temp.txt are relative to my application path (note: app.exe isn't the name of my C# application, it's just a program I'm using for the Process), but _patchpath is an absolute path like D:\blah\file.ips. The problem is, the process doesn't work (_patchpath is supposed to be patched with the file temp.txt) if its absolute, but does work if its relative to my app directory. Why is that and how can I fix it?
If I need to be clear please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调试此类问题的常用方法是使用调试器。将其复制/粘贴到“app.exe”源文件的 Main() 方法中:
app.exe 开始运行后,您将看到一个对话框,让您选择调试器。从那里你应该不会有太多困难找出它不起作用的原因。
如果您没有 app.exe 的源代码,那么您需要仔细考虑。使用“app.exe”或“temp.txt”等相对路径总是很麻烦。经典的失败模式是使用 OpenFileDialog 让用户选择 _patchpath 值。如果该对话框的 RestoreDirectory 属性未设置为 True,则程序的默认目录将更改为补丁文件的路径。 app.exe 和 temp.txt 都不再是第四个了。
通过防御性编程来保护自己免受这种情况的影响:
The usual approach to debug problems like this is to use the debugger. Copy/paste this into the Main() method of the source file for 'app.exe':
As soon as app.exe starts running, you'll get a dialog that lets you pick a debugger. From there you shouldn't have much trouble figuring out why it doesn't work.
If you don't have the source code for app.exe then you'll need to think this through. Using a relative path like "app.exe" or "temp.txt" is always trouble. A classic failure mode is using an OpenFileDialog to let the user pick the _patchpath value. If that dialog's RestoreDirectory property isn't set to True then your program's default directory changes to the path of the patch file. And neither app.exe nor temp.txt can be fournd anymore.
Protect yourself against this by programming defensively:
问题很可能是被调用的应用程序 (app.exe) 不理解这些参数。解决此问题的最佳方法是使用您提供的参数来调试 app.exe,以防它不起作用。尝试将 app.exe 的调试器中的参数设置为与失败案例完全相同的参数,并检查解析参数所产生的变量。
The problem is most likely that the called application (app.exe) does not understand the parameters. The best way to solve this issue would be to debug app.exe with the parameters that you provide in the case it doesn't work. Try to set the arguments in the debugger for app.exe to exactly the same parameters as the failed case, and inspect the variables that result from parsing the arguments.