开发者

Find a application path

How I can find path for installed application from VB.net if i kno开发者_高级运维w the name of application exe file? Thanks!


If you mean any arbitrary application (not the one that's running), there's really no other way than to search the hard drive for that .exe's file name.

If you can at least be confident that it's in the Program Files folder, this should give you what you need.

string[] files = System.IO.Directory.GetFiles(
            Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), 
            "Yourexe.exe", System.IO.SearchOption.AllDirectories);

This will give you back a string array of the paths to all files that have that name within the Program Files folder.

If you can't assume that it's in Program Files, then you can substitute any directory.

string rootDirectory = System.IO.DriveInfo.GetDrives()[0].RootDirectory.FullName;

string[] files = System.IO.Directory.GetFiles(
            rootDirectory, 
            "Yourexe.exe", System.IO.SearchOption.AllDirectories);

That will search the entire primary hard drive. Again, you can substitute any root path you like for the rootDirectory variable.

It should be noted that these options are relatively slow; there is, unfortunately, no fast way to accomplish this without knowing more information about where it might be installed.


This is how you find the path of the executing assembly if that is what you are looking for?

String strPath = System.IO.Path.GetDirectoryName(
 System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

[Visual Basic]
Dim strPath As String = System.IO.Path.GetDirectoryName( _
 System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

This link will show you how to find other files paths if you know the name of the file:

http://csharp.net-tutorials.com/file-handling/file-and-directory-information/

Specifically, here is a code snippet from the above link to find executable files in directories:

FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.AllDirectories);

You can replace: "*.exe" with the name of your executable.


If your code executes inside application.exe, you can get the location of the file (application.exe) by calling Assembly.GetExecutingAssembly().Location.


If I understand you correctly, you want to find an application that is installed on your machine, but not nec. the application that's running? If so, you can check the registry...

Sorry this is in C#, I'm not familiar with VB.Net.

public string GetApplicationPath(string appname)
{
    using(Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, ""))
    {
        using(Microsoft.Win32.RegistryKey subkey = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + appname))
        {
            if(subkey == null)
               return "";

            object path = subkey.GetValue("Path");

            if(path!=null)
              return (string)path;
        }

    }
    return "";
}

Then you can call this by using:

string path = GetApplicationPath("Myexename.exe");


As the others already mentioned, if you're searching for the path of yourself it is quite easy. If you need the path of an arbitary running app you can try something like this:

var procList = Process.GetProcesses().Where(process => process.ProcessName.Contains("notepad"));

foreach (var process in procList)
{
    Console.WriteLine("Path to {0}: {1}", process.ProcessName, Path.GetDirectoryName(process.MainModule.FileName));
}

When you need some other information about the process you should take a look into the Process class.

If the application you're looking for is not running, you can try a search on Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).

Or you can try a search within the registry path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall to enumerate through all programs that are listed in Control Panel - Add / Remove programs.

If all of these steps doesn't help to find your wanted application there is no other way (as far as i know) than doing a full scan of the hard drive.


If you are referring to the startup path of your application:

Application.StartupPath()
  • note: the above won't work if you are trying to access it via a referenced assembly

If you are referring to searching for another application via its name, then I don't think there is anything built in. You would have to iterate through files on the local machine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜