简体   繁体   English

使用System.Diagnostics.Process.Start()启动程序时遇到问题

[英]Trouble launching program with System.Diagnostics.Process.Start()

What should be a trival matter has now gobbled up an hour of my time! 什么应该是一个微不足道的事情现在吞噬了我一小时的时间! >__< > __ <

I want to start an executable with a xml path as an argument. 我想以xml路径作为参数启动可执行文件。 (I've added the directory in which this program resides to my system path.) Seems simple enough. (我已将此程序所在的目录添加到我的系统路径中。)看起来很简单。 My first approach was using the static Process.Start() method as such: 我的第一种方法是使用静态Process.Start()方法:

Process.Start(@"MyExecutable.exe", "C:\\\\My Doc\\\\SomeDirectory\\\\MyXMLPath.xml");

The process does start, but like half a second later it dies. 这个过程确实开始了,但是半秒后它就会死掉。 So, I'm like ooookaay , maybe the executable doesn't like the argument I'm giving it? 所以,我喜欢ooookaay ,也许可执行文件不喜欢我给它的论点? Just for giggles, I created a shortcut to the executable and added the xml file path as one of it's arguments. 只是为了咯咯笑,我创建了一个可执行文件的快捷方式,并添加了xml文件路径作为其中一个参数。 The program starts and runs as expected. 程序启动并按预期运行。 Not sure why this works, I decided to test my luck on the command line as well: 不知道为什么会这样,我决定在命令行测试我的运气:

C:\\MyExecutable.exe "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml"

No problem starting this way too. 从这个方面开始也没问题。

Now, at this point I started grasping for straws and decided to create an instance of the Process class: 现在,在这一点上,我开始抓住吸管,并决定创建一个Process类的实例:

Process proc = new Process();
proc.StartInfo.FileName = @"MyExecutable.exe";
proc.StartInfo.Arguments = "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml";
proc.Start();

Needless to say this didn't help at all. 毋庸置疑,这根本没有帮助。 >.< > <

So frusterated, I decided test my luck commenting out one line: 如此冷酷,我决定测试我的运气评论一行:

//proc.StartInfo.Arguments = "C:\\\\My Docs\\\\SomeDirectory\\\\MyXMLPath.xml";

And the process starts. 这个过程开始了。 Not with its necessary arguments, but it starts. 没有必要的论据,但它开始了。 So, the question is, why is the program not accepting the path I give it when I try to launch it through the Process class? 所以,问题是,当我尝试通过Process类启动它时,为什么程序不接受我给它的路径? If only it left me a message when the program died. 如果只是在程序死亡时给我留言。 :( :(

Any thoughts? 有什么想法吗? Much appreciated! 非常感激!

The problem might be because of the spaces in the file path. 问题可能是因为文件路径中的空格。 If you think about it in how you made the DOS call you had to put quotes around the path. 如果你考虑如何进行DOS调用,你必须在路径上加上引号。 But the way you are calling you are not. 但你打电话给你的方式不是。 So try adding single quotes around the path. 因此,尝试在路径周围添加单引号。 That should take care of it. 那应该照顾它。 If you think about it from the standpoint of how that would look rendered as a command line then it makes more sense why you need to do that. 如果您从如何将其呈现为命令行的角度考虑它,那么为什么需要这样做更有意义。

Process proc = new Process();
proc.StartInfo.FileName = @"MyExecutable.exe";
proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\"";
proc.Start();

Your arguments contain a space: 您的参数包含空格:

proc.StartInfo.Arguments = "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml";

This means that the program you're executing is receiving the following as its arguments: 这意味着您正在执行的程序正在接收以下内容作为其参数:

  • C:\\My
  • Docs\\SomeDirectory\\MyXMLPath.xml

What you need to do is wrap it in double quotes (not single as suggested in another answer), thusly: 您需要做的是用引号括起来(不是另一个答案中建议的单引号),因此:

proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\"";

which means that the program you're executing will now receive only one argument: 这意味着您正在执行的程序现在只会收到一个参数:

  • "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml"

You should put in a handler for the Exited event of the process: 您应该为流程的Exited事件添加一个处理程序:

        Process p = new Process();
        p.Exited += new EventHandler(MyProcess_Exited);

and then check the ExitCode inside the handler: 然后检查处理程序内的ExitCode

    private void MyProcess_Exited(object sender, EventArgs e)
    {
        Process p = sender as Process;

        if (p.ExitCode == someValue)......
    }

in the absense of any other info i would say that the privileges of the started process are insufficient to read or access the xml file. 在没有任何其他信息的情况下,我会说启动过程的特权不足以读取或访问xml文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM