Вопрос

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 ;)

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top