2016-03-07 29 views
-5

是否有任何方式可以得到exe的路径?我的意思是想我在我的本地磁盘CI 20个前男友想得到这样的exe文件的路径“C:\ MYEXE.EXE也可以是C:\目录\ MYEXE.EXE”如何让我的计算机中的所有exe文件的路径与c#

string getpathforexe[] = themethord; 
foreach(string printvalue in getpathforexe) 
{ 
    messagebox.show(printvalue.tostring()); 
} 

等什么将成为主题?

+0

提示:看看'Directory'类:https://msdn.microsoft.com/en-us/library/system.io.directory –

+0

是不是Directory.GetFiles方法(字符串,字符串,SearchOption )? – arn48

+0

那你试过了吗? –

回答

2

你基本上需要的是一个安全的,递归的,.exe根文件夹的搜索功能,你可以将它应用到任何地方。

事情是这样的:

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 
    } 
} 

要小心两件事的:

  1. 并非所有的目录都可以访问。当您尝试访问无法访问的目录时,您将获得UnauthorizedAccessException
  2. 通过使用递归搜索,您希望您的搜索结果失败只有在您没有访问权限的目录中。

不过,如果将此应用于所有文件夹,可能需要很长时间。最好将其应用于您要搜索.exe文件的特定文件夹。

说明:

在功能方面,给出的路径,如果先列出的目录中顶层文件夹中的文件:

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 
} 

如果任何目录包含的任何子文件夹中,做递归搜索到孩子的目录,并将结果在dirPathList加在一起,最后返回它:

return dirPathList; //return the whole list found at this level 

然后你可以得到所有的文件与".exe"

而且在抓,你可能要检查是否有未经授权的访问异常:

catch (UnauthorizedAccessException ex){ 
    //Do something 
    return null; //something has gone wrong, return null 
} 

你使用这样的:

List<string> exefiles = YourClassNameWhoHasTheMethod.GetAllAccessibleFiles(testfolder, "*.exe")); 

例如,你可以使用回收站测试像这样:

string rbin = @"C:\$Recycle.Bin"; 
List<string> files = GetAllAccessibleFiles(rbin, "*.exe"); 

这些是我得到的文件:

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 
+0

这似乎是一个相当长的替代'Directory.GetFiles(“c:\\”,“*。exe”,SearchOption.AllDirectories)'... –

+0

我害怕不可访问目录,@JonSkeet先生。如果我们把'Directory.GetFiles(“c:\\”,“* .exe”,SearchOption.AllDirectories)',那么当其中一个目录搜索失败时,我们不能得到其余的。 – Ian

+0

是什么让你觉得'Directory.GetFiles'不能处理呢? (请注意,您正在使用'GetAllAccessibleDirectories'方法,但未显示......这是干什么的,以及如何操作?)(一些实验显示它真的*不处理它,这让我感到吃惊 - 但是您应该说明你在回答海事组织问题时,以及如何避免它。) –

相关问题