[英]How to get all the paths of exe in my computer with c#
Yes there any way by which i can get the paths of exe?是的,我可以通过什么方式获得exe的路径? I mean suppose i have 20 exes in my local disk ci want to get the paths of the exe like "C:\\myexe.exe or it can be C:\\dir\\myexe.exe"我的意思是假设我的本地磁盘 ci 中有 20 个 exe 想要获取 exe 的路径,例如“C:\\myexe.exe 或者它可以是 C:\\dir\\myexe.exe”
string getpathforexe[] = themethord;
foreach(string printvalue in getpathforexe)
{
messagebox.show(printvalue.tostring());
}
so what will be themethord?那么什么是themethord?
What you essentially need is a safe, recursive, .exe
search functions on a root folder, and you can apply it anywhere.您本质上需要的是根文件夹上的安全、递归、 .exe
搜索功能,您可以在任何地方应用它。
Something like this:像这样的东西:
public static List<string> GetAllAccessibleFiles(string path, string searchPattern) {
List<string> dirPathList = new List<string>();
try {
string[] childFilePaths = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
dirPathList.AddRange(childFilePaths);
foreach (string childDirPath in Directory.GetDirectories(path)) { //foreach child directory, do recursive search
List<string> grandChildDirPath = GetAllAccessibleFiles(childDirPath, searchPattern);
if (grandChildDirPath != null && grandChildDirPath.Count > 0) //this child directory has children and nothing has gone wrong
dirPathList.AddRange(grandChildDirPath); //add the grandchildren to the list
}
return dirPathList; //return the whole list found at this level
} catch (UnauthorizedAccessException ex){
//Do something if necessary
return null; //something has gone wrong, return null
}
}
Be careful of a couple of things:请注意以下几点:
UnauthorizedAccessException
当您尝试访问无法访问的目录时,您会收到UnauthorizedAccessException
Nevertheless, if you apply this to all your folders, likely you will take very long time.不过,如果您将此应用于所有文件夹,则可能需要很长时间。 It is best to apply it to particular folders which you want to search for .exe
files.最好将其应用于要搜索.exe
文件的特定文件夹。
Explanations:说明:
In the function, given a path, if first lists the files in the directory top folder:在函数中,给定路径,如果先列出目录top文件夹中的文件:
string[] childFilePaths = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
dirPathList.AddRange(childFilePaths);
If any of your files matches your search pattern, you add those files.如果您的任何文件与您的搜索模式匹配,则添加这些文件。 Then next you check each folder consisted in your path directory:然后接下来检查路径目录中包含的每个文件夹:
foreach (string childDirPath in Directory.GetDirectories(path)) { //foreach child directory, do recursive search
List<string> grandChildDirPath = GetAllAccessibleFiles(childDirPath, searchPattern);
if (grandChildDirPath != null && grandChildDirPath.Count > 0) //this child directory has children and nothing has gone wrong
dirPathList.AddRange(grandChildDirPath); //add the grandchildren to the list
}
If any of the directory consists of any child folders, do recursive search to the children directories, and add the results together in the dirPathList
and finally returns it:如果任何目录包含任何子文件夹,则对子目录进行递归搜索,并将结果一起添加到dirPathList
,最后返回它:
return dirPathList; //return the whole list found at this level
Then you could get all the files with ".exe"
.然后你可以得到所有带有".exe"
的文件。
And in the catch, you probably want to check for the unauthorized access Exception:在捕获中,您可能想要检查未经授权的访问异常:
catch (UnauthorizedAccessException ex){
//Do something
return null; //something has gone wrong, return null
}
You use it like this:你像这样使用它:
List<string> exefiles = YourClassNameWhoHasTheMethod.GetAllAccessibleFiles(testfolder, "*.exe"));
For example, you could test it with Recycle Bin like this:例如,您可以使用回收站进行测试,如下所示:
string rbin = @"C:\$Recycle.Bin";
List<string> files = GetAllAccessibleFiles(rbin, "*.exe");
And these are what I get for the files:这些是我从文件中得到的:
C:\$Recycle.Bin\S-1-5-21-3161714743-1342575415-982792061-1001\$IYFMY6V.exe
C:\$Recycle.Bin\S-1-5-21-3161714743-1342575415-982792061-1001\$RYFMY6V.exe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.