查找应用程序路径

use*_*271 1 .net c# vb.net

如果我知道应用程序exe文件的名称,如何从VB.net找到已安装应用程序的路径?谢谢!

kem*_*002 5

如果您正在寻找的话,这就是您如何找到执行程序集的路径?

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)
Run Code Online (Sandbox Code Playgroud)

如果您知道文件的名称,此链接将显示如何查找其他文件路径:

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

具体来说,这是上面链接中的代码片段,用于查找目录中的可执行文件:

FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.AllDirectories);
Run Code Online (Sandbox Code Playgroud)

您可以将"*.exe"替换为可执行文件的名称.


Ada*_*son 5

如果您指的是任意应用程序(而不是正在运行的应用程序),则除了在硬盘驱动器中搜索该.exe的文件名之外,实际上没有其他方法。

如果您至少可以确定它位于“程序文件”文件夹中,则可以为您提供所需的信息。

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

这将为您提供“程序文件”文件夹中所有具有该名称的文件的路径的字符串数组。

如果不能假定它位于“程序文件”中,则可以替换任何目录。

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

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

那将搜索整个主硬盘。同样,您可以将所需的任何根路径替换为rootDirectory变量。

应当指出,这些选择相对较慢;还有,不幸的是,没有快速的方式在不知道有关它的更多信息做到这一点可能会被安装。