0

有些人和我在我们的 C 驱动器中存储了相同的文件夹,但每个文件夹位于不同的位置。我正在尝试编写可以在每台机器上访问此文件夹的代码。我试过"~/xx/xx"了,但它不起作用。我该怎么做?

更新: 这就是我现在拥有的:

foreach (var dir in Directory.EnumerateDirectories(@"C:\", "*dataDir*", SearchOption.AllDirectories))
{
     Debug.WriteLine(dir);
}

例外:System.UnauthorizedAccessException: Access to the path 'C:\$INPLACE.~TR\' is denied.

4

2 回答 2

2

Directory.EnumerateDirectories应该可以工作。.Single()如果您只期待一场比赛,您可以这样做。

foreach(var dir in Directory.EnumerateDirectories(@"C:\", "*STUFF*", SearchOption.AllDirectories))
{
    Console.WriteLine(dir);
}
产量:
C:\东西
C:\其他\东西

于 2012-08-17T17:52:14.793 回答
0

您可以在Directory.GetDirectories Method (String, String)中尝试此示例

using System;
using System.IO;

public class Test 
{
   public static void Main() 
   {
       try 
       {
           // Only get subdirectories that begin with the letter "p." 
           string[] dirs = Directory.GetDirectories(@"c:\", "p*");
           Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
           foreach (string dir in dirs) 
           {
               Console.WriteLine(dir);
           }
       } 
       catch (Exception e) 
       {
           Console.WriteLine("The process failed: {0}", e.ToString());
       }
   }

}

于 2012-08-17T17:58:24.143 回答