문제

I have a filedialog in my application that gets the path of the file to execute, for example.

C:\filespool\run.exe

Now I put this in a string variable called exepath and execute it with this code

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = path;
proc.StartInfo.FileName = exepath;
proc.Start();

You can see that I've set the workingdirectory but I dont know how to get it in the best way, so I ask people that know it here. How to get the workingdirectory "C:\filespool".

도움이 되었습니까?

해결책

You can create a FileInfo object and reference its DirectoryName property. You'll have to include the System.IO namespace.

FileInfo f = new FileInfo(exepath);
string path = f.DirectoryName;

Here's the documentation.

다른 팁

I'm not sure what you need fully, but you can use FileInfo.Directory to get the directory of a file path.

Here is one answer. It is something that can trip you up if you are not ready for it. After an open (or save) dialog box, Environment.CurrentDirectory changes to the directory of the dialog. So:

path = Environment.CurrentDirectory;
exepath = dlg.FileName;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top