سؤال

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