2012-12-10 43 views
1

我遇到了尝试通过搜索文件结构来创建列表的问题。正在尝试制作一个基本的C#控制台程序,它只会运行并执行此操作。在文件结构中搜索以计算子文件夹中的项目

我的结构组织如下。

网上\
X1 \ 用户\ (很多很多的用户文件夹)\ 搜索一个特定的子文件夹\ 此子文件夹中作出的任何文件夹中的一个文本文件列表

所以我必须能够搜索每个用户文件夹,然后检查一个文件夹(这将每次都是相同的)然后在下面的格式中创建该子文件夹中找到的文件夹的列表

username(name用户文件夹)>>特定文件夹内的文件夹名称。

它已经很长时间了,因为我不得不在文件结构中搜索任何东西,所以在这么糟糕的情况下如此消隐。

** * ** * ** * ** * ****编辑!!!!!

感谢您的信息和链接。现在正在研究这个问题,但是想知道这是否合理并且可行。不要在我确定它看起来像不会让事情搞砸之前进行测试。

  TextWriter outputText = new StreamWriter(@"C:\FileList.txt", true); 
     outputText.WriteLine("Starting scan through user folder"); 
     string path = @"\\X1\users"; 
     string subFolder = "^^ DO NOT USE - MY DOCS - BACKUP ^^"; 
     string [] user_folders = Directory.GetDirectories(path); 

     foreach (var folder in user_folders) 
     { 
      string checkDirectory = folder + "\\" + subFolder; 

      if (Directory.Exists(checkDirectory) == true) 
      { 
       string [] inner_folders = Directory.GetDirectories(checkDirectory); 
       foreach (var folder2 in inner_folders) 
       { 
        outputText.WriteLine(folder2); 
       } 
      } 
     } 
     outputText.WriteLine("Finishing scan through user folder"); 
     outputText.Close(); 

固定和工作!!!!不得不改变字符串[]行,使它.GetDirectories而不是.GetFiles!

+2

查看应该对你有帮助的'Directory'和'DirectoryInfo'类。 –

+0

我相信我下面发布的第二种方法将比手动遍历所有用户更快 –

+0

我的修改后的代码显然根本不起作用。我认为这可能是我未被引用的原始路径。此路径位于随机网络驱动器上。投入编辑我现在拥有的东西。 – Kevin

回答

2

由于巴厘岛C提到,目录类将是你的朋友在这一个。下面的例子应该让你开始。

来源:http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/3ea19b83-b831-4f30-9377-bc1588b94d23/

//Obviously you'll need to define the correct path. 
string path = @"My Network\X1\Users\(many many user folders)\Search for a specific sub folder \"; 

// Will Retrieve count of all files in directry and sub directries 
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; 

// Will Retrieve count of all files in directry but not sub directries 
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length; 

// Will Retrieve count of files .txt extensions in directry and sub directries 
int fileCount = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories).Length; 

如果你需要搜索/用户/文件夹的某些人,或者某些条件下,你可以做到以下几点:

string path = @"PATH_TO_USERS_DIRECTORY"; 
string [] user_folders = Directory.GetFiles(path); 
foreach(var folder in user_folders) 
{ 
    if folder == "MyFolder"; 
     Process(folder); //Search the directory here. 
} 
+0

是否可以将通配符值放入路径字段中?或者我将不得不搜索远程用户......然后为用户中的每个文件执行搜索特定文件夹。然后另一个列出该文件夹中的文件夹? – Kevin

+0

如果您只是试图浏览/ Users /中的所有文件夹,则可以使用“SearchOption.AllDirectories”选项。如果您正在寻找特定用户或想要筛选某些用户,则可以在/ Users /中获取所有文件并遍历它们,只搜索您想要的文件。我会附加到答案。 – JoshVarty

+0

感谢您的信息,我发布了一个编辑到我的原始问题(显然我不能回答我自己的问题)与迄今为止我不知道,如果它是有道理的,或者如果我写我想要的方式。 – Kevin

1

尝试以下操作执行。这将只写到控制台:

const string root = "<<your root path>>"; 
const string directoryToLookFor = "<<the folder name you are looking for>>"; 
foreach (var directory in Directory.EnumerateDirectories(root, "*.*", SearchOption.TopDirectoryOnly)) 
{ 
    var foundDirectory = Directory.EnumerateDirectories(directory, directoryToLookFor, SearchOption.TopDirectoryOnly).FirstOrDefault(); 
    if (!String.IsNullOrEmpty(foundDirectory)) 
    { 
     var filesInside = Directory.GetFiles(foundDirectory); 
     foreach (var file in filesInside) 
     { 
      Console.WriteLine(file); 
     } 
    } 
} 

或者你可以只是做:

foreach (var foundDirectory in Directory.EnumerateDirectories(root, directoryToLookFor, SearchOption.AllDirectories)) 
{ 
    var filesInside = Directory.GetFiles(foundDirectory); 
    foreach (var file in filesInside) 
    { 
     Console.WriteLine(file); 
    } 
} 

这将所有子目录中进行搜索,您无需遍历用户的文件夹。

+0

哦,在我设法自己完成之前没有看到这个。再次阅读上面的评论,并认为这是其他人的帖子。在页面中间添加我只是没有下去。如果我改变文件查找目录像我不得不为我的我相信这也会工作。 – Kevin

+0

哦,我明白了。那么我粘贴上面的代码工作(至少在我做的基本测试中),检查它看看它是否有用。 –