Process.Start, WorkingDirectory, start one exe from another

被刻印的时光 ゝ 提交于 2019-12-20 07:16:28

问题


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
  • 2 exe files: MainFolder/MainProg.exe and /Sub2/SecondProg.exe
  • MainProg.exe is not from me, I wrote an .cs file inside /Sub1, kind of a plugin. (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 ?!)
  • Start MainProg.exe -> Click a button -> Start SecondProg.exe
  • SecondProg.exe reads values from an xml file
  • Both are .NET 4.5

Problem:

  • I want to have the xml file inside /Sub2
  • If I do that and start SecondProg.exe the normal way, double clicking it, everything is working fine
  • If I try to start SecondProg.exe via the button in MainProg.exe, I get "SecondProg is not working anymore" from windows.
  • If I copy the xml file to MainFolder, its working..

So, I am still learning C#, but could there be a problem with the workingdirectory? 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...

Inside my .cs file, I start the SecondProg via

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. well.. its hard to describe if you're not using your native language, but I hope you get what I mean ;)


回答1:


That SecondProg.exe apparently looks in its working directory for that file and fails if it doesn't find it. 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. You then pass that object as an argument when you call Process.Start.



来源:https://stackoverflow.com/questions/23532451/process-start-workingdirectory-start-one-exe-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!