简体   繁体   English

如何用c#获取我电脑中exe的所有路径

[英]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:请注意以下几点:

  1. Not all directories are accessible.并非所有目录都可以访问。 When you try to access an unaccessible directory, you would get UnauthorizedAccessException当您尝试访问无法访问的目录时,您会收到UnauthorizedAccessException
  2. By using recursive search, you want your search result to be failed only on the directory where you have no access right.通过使用递归搜索,您希望搜索结果在您没有访问权限的目录上失败。

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.

相关问题 在另一台计算机上运行我的C#exe? - Running my c# exe on another computer? 我可以在租用/云计算机上运行C#.exe吗? - Can I run my C# .exe on a rented / cloud computer? 我怎么知道我的exe在C#中的用户计算机中的位置 - how do i know where my exe is located in users computer in C# (c#) 如何检查某个 exe 是否位于用户计算机上? - (c#) How to check if a certain exe is located on a users computer? 如何通过我的C#程序在我的计算机上打开所有* .xml文件? - how to open all *.xml files on my computer by my C# program? C# + .NET:在电脑中找到chrome.exe之类app的路径(所有磁盘,所有目录) - C# + .NET : Find the path of an app like chrome.exe in the computer (all disks, all directories) c#WIQL查询以获取所有不同的迭代路径 - c# WIQL Query to get all different Iteration Paths 在客户端计算机上安装我的c#项目的.exe文件时出现“连接到SQL服务器”错误 - Getting 'connection to SQL server' error while installing a .exe file of my c# project on client's computer 如何在C#中使用WMI查询获取计算机的物理内存? - How do i get Physical memory of my computer using WMI query in C#? 如何在C#中消除错误``并非所有代码路径都返回值&#39;&#39; - How to remove the error 'not all code paths return a value' in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM