Reputation: 279
How can I get the full path of an application? For example, I want to get the path of windiff:
C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\WinDiff.Exe
My desired result is:
C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin
I tried this, but won't work.
string fileName = "windiff.exe";
FileInfo f = new FileInfo(fileName);
MessageBox.Show(f.FullName.ToString());
Upvotes: 2
Views: 3042
Reputation: 586
The installation folder of Windows SDK is set in the registry here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A\InstallationFolder
You could check for several versions of Windows SDK by changing v7.1A to v8.0A for example.
This is a code I use for getting to the folder:
var sdkRootPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v7.1A", "InstallationFolder", null) as string;
if (null == sdkRootPath)
throw new InvalidOperationException("Could not find Windows SDK 7.1A installation folder.");
return sdkRootPath;
Upvotes: 1
Reputation: 7202
See the other answers for help on recursively enumerating directories looking for "windiff.exe". Also keep in mind that you might find multiple hits, so you'll need some logic to deal with that.
The higher-level problem here seems to be needing to show the user a diff of two files.
I'd approach this by shipping a default diff tool with my application instead of trying to find the Windows SDK on whichever drive it was installed on, assuming it was installed at all. That is the tool I'd use by default. You can write one on your own or you can, perhaps working with the lawyers :-], include an open source tool.
You could also provide the user an option to configure their favorite diff tool. This is the approach taken by many source control systems.
Upvotes: 1
Reputation: 43023
If you need to find a file in some location, you can use the code below:
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] files = di.GetFiles("WinDiff.Exe", SearchOption.AllDirectories);
files
array will then contain the files that are found. You can then use DirectoryName
property to get the folder for each file.
Warning though: when using this way on C:\ drive especially, some folders will not be accessible. You should narrow your search.
Upvotes: -1
Reputation: 6039
FileInfo
is not going to magically find the WinDiff.exe for you somewhere on the hard drive. It is a utility class that 'Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files' (http://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx)
You will have to find the file by either having the user browse for it or search the hard drive(s) programmatically.
Here is an example of how to iterate through a directory tree: http://msdn.microsoft.com/en-us/library/bb513869.aspx
Beware, however, not all users appreciate/trust a program that trolls through their hard drive(s) unannounced!
Upvotes: 0