简体   繁体   English

Process.Start,WorkingDirectory,从另一个启动一个exe

[英]Process.Start, WorkingDirectory, start one exe from another

I hope you will get my problem, dont know if I can describe it properly in english, but I will try :) 希望您能解决我的问题,不知道我是否可以用英语正确描述,但我会尝试:)

Situation: 情况:

  • Folder Structure: MainFolder/Sub1/Sub2 文件夹结构:MainFolder / Sub1 / Sub2
  • 2 exe files: MainFolder/MainProg.exe and /Sub2/SecondProg.exe 2个exe文件:MainFolder / MainProg.exe和/Sub2/SecondProg.exe
  • MainProg.exe is not from me, I wrote an .cs file inside /Sub1, kind of a plugin. MainProg.exe不是我的,我在/ Sub1里面写了一个.cs文件,有点像插件。 (I think MainProg.exe kind of works like a compiler, cause it has a log window where I can see errors from my .cs file.. I think the author somewhere mentioned a JIT compiler ?!) (我认为MainProg.exe的工作方式类似于编译器,因为它有一个日志窗口,从中可以查看.cs文件中的错误。。我认为作者在某处提到了JIT编译器?!)
  • Start MainProg.exe -> Click a button -> Start SecondProg.exe 启动MainProg.exe->单击按钮->启动SecondProg.exe
  • SecondProg.exe reads values from an xml file SecondProg.exe从xml文件读取值
  • Both are .NET 4.5 两者都是.NET 4.5

Problem: 问题:

  • I want to have the xml file inside /Sub2 我想在/ Sub2中包含xml文件
  • If I do that and start SecondProg.exe the normal way, double clicking it, everything is working fine 如果我这样做,然后以正常方式启动SecondProg.exe,则双击它,一切正常
  • If I try to start SecondProg.exe via the button in MainProg.exe, I get "SecondProg is not working anymore" from windows. 如果我尝试通过MainProg.exe中的按钮启动SecondProg.exe,则会从Windows中收到“ SecondProg不再起作用”的信息。
  • If I copy the xml file to MainFolder, its working.. 如果我将xml文件复制到MainFolder,则其工作正常。

So, I am still learning C#, but could there be a problem with the workingdirectory? 因此,我仍在学习C#,但是workdirectory可能有问题吗? I am so confused, because the MainProg.exe has nothing to do with my xml file, it doesnt even know its there, the only point, where I use it is when loading values into SecondProg.exe... 我很困惑,因为MainProg.exe与我的xml文件无关,它甚至不知道它在那儿,唯一的一点,我使用它的地方是在将值加载到SecondProg.exe中时...

Inside my .cs file, I start the SecondProg via 在我的.cs文件中,我通过以下方式启动SecondProg

public override void Button()
{
Process.Start("Sub1\\Sub2\\SecondProg.exe");
}

So its like, MainProg has the button, in my .cs file I tell him what to onClick. 就像MainProg一样,在我的.cs文件中有按钮,我告诉他onClick。 well.. its hard to describe if you're not using your native language, but I hope you get what I mean ;) 好..很难描述您是否不使用母语,但我希望您明白我的意思;)

That SecondProg.exe apparently looks in its working directory for that file and fails if it doesn't find it. 该SecondProg.exe显然在其工作目录中查找该文件,如果找不到该文件,则失败。 As such, you have to set the working directory of the new process. 这样,您必须设置新进程的工作目录。 To do that, create a ProcessStartInfo object, set the FileName to the path of the EXE file and the WorkingDirectory to the path of the folder containing that EXE. 为此,创建一个ProcessStartInfo对象,将FileName设置为EXE文件的路径,并将WorkingDirectory设置为包含该EXE的文件夹的路径。 You then pass that object as an argument when you call Process.Start . 然后,在调用Process.Start时,将该对象作为参数传递。

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

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