我想列出该目录的目录和子目录中包含的每个文件和目录.如果我选择C:\作为目录,程序将获得它有权访问的硬盘驱动器上的每个文件和文件夹的每个名称.
列表可能看起来像
fd\1.txt fd\2.txt fd\a\ fd\b\ fd\a\1.txt fd\a\2.txt fd\a\a\ fd\a\b\ fd\b\1.txt fd\b\2.txt fd\b\a fd\b\b fd\a\a\1.txt fd\a\a\a\ fd\a\b\1.txt fd\a\b\a fd\b\a\1.txt fd\b\a\a\ fd\b\b\1.txt fd\b\b\a
可能重复:
.NET - 检查目录是否可访问而不进行异常处理
我使用.NET 3.5和C#在Visual Studio 2010中制作一个小文件浏览器,我有这个功能来检查目录是否可访问:
RealPath=@"c:\System Volume Information";
public bool IsAccessible()
{
//get directory info
DirectoryInfo realpath = new DirectoryInfo(RealPath);
try
{
//if GetDirectories works then is accessible
realpath.GetDirectories();
return true;
}
catch (Exception)
{
//if exception is not accesible
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
但我认为对于大目录,尝试让所有子目录检查目录是否可访问可能会很慢.我在尝试探索受保护的文件夹或没有光盘的cd/dvd驱动器时使用此功能来防止错误("设备未就绪"错误).
是否有更好的方法(更快)检查应用程序是否可以访问目录(最好是在NET 3.5中)?
当我使用opendir,readdir和读取Perl中的目录时closedir,该readdir函数似乎没有以任何特定顺序读取文件(我可以告诉).
我正在读取一个目录,该目录包含以epoch timestamp命名的子目录:
1224161460
1228324260
1229698140
Run Code Online (Sandbox Code Playgroud)
我想以数字顺序读取这些目录,这将把最旧的目录放在第一位.
当我使用时readdir,它读取的第一个是1228324260,这是中间的一个.我知道我可以将目录内容放在一个数组中并对数组进行排序,但是我可以传递一个选项readdir来按排序顺序读取吗?或者可能是一种更优雅的方法来实现这一点,而不是将所有内容推入数组并对数组进行排序 可能还有模块可以做到这一点,但很难在我们的环境中安装模块,所以除非它是一个内置模块,否则我宁愿不使用模块......
谢谢!
编辑 根据要求,我发布了我正在使用的代码:
opendir( my $data_dh, $data_dir ) or die "Cannot open $data_dir\n";
while ( my $name = readdir($data_dh) ) {
next if ( $name eq '.' or $name eq '..' );
my $full_path = "${data_dir}/${name}";
next unless ( -d $full_path );
process_dir($full_path);
}
closedir($data_dh);
Run Code Online (Sandbox Code Playgroud) 我使用DirectoryInfo.GetDirectories()递归方式查找给定路径下的所有子目录.但是,我想排除系统文件夹,没有明确的方法.在FindFirstFile/FindNextFile中,属性更清晰.
虽然在未提及的文件,Directory.GetDirectories()似乎总是返回目录名的字典顺序排序阵列.依赖这个实现细节是否安全(它适合我的需要)还是应该偏执并根据需要对我的目录列表进行排序?
[Test]
public void SortedDirectories()
{
string[] directories = Directory.GetDirectories(@"C:\Windows");
Assert.That(directories, Is.Ordered);
}
Run Code Online (Sandbox Code Playgroud) 当我尝试访问我的c:驱动器的子目录时,我有一个奇怪的情况:
首先我尝试了以下代码,输出为0(零):
MessageBox.Show(new DirectoryInfo("c:").GetDirectories().Length.ToString());
但是当路径(c :)添加'\'时,它显示了c:drive中子文件夹的确切数量.
MessageBox.Show(new DirectoryInfo("c:\\").GetDirectories().Length.ToString());
但尝试了另一个驱动器(d :)像:
MessageBox.Show(new DirectoryInfo("d:").GetDirectories().Length.ToString());
它检索所有子目录.
谁能解释为什么会这样?
谢谢你们.现在我明白了"c:"返回当前目录而不是root "c:\".但我没有得到任何错误,如所提到的那样.
我试图在"c:\ Users"中迭代窗口中的用户文件夹列表但排除了microsoft内置用户文件夹,下面是我用来完成此专长的代码段但是由于某种原因不按预期工作.
private readonly List<String> _exclusion = new List<String>
{
"All Users",
"Default",
"LocalService",
"Public",
"Administrator",
"Default User",
"NetworkService"
};
public static bool FoundInArray(List<string> arr, string target)
{
return arr.Exists(p => p.Trim() == target);
}
foreach (string d in Directory.GetDirectories(sDir).Where(d => !FoundInArray(_exclusion,d)))
{
richTextBox1.Text += d + Environment.Newline;
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这不起作用,有人可以为我提供一些见解吗?
我想返回'SomeFolder'目录中所有子目录的列表,不包括 'Admin'和'Templates'目录.
我有以下文件夹结构(简化):
C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString
C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString
C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString
C:\inetpub\wwwroot\MyWebsite\SomeFolder\Admin
C:\inetpub\wwwroot\MyWebsite\SomeFolder\Templates
Run Code Online (Sandbox Code Playgroud)
'SomeFolder'可以包含不同数量的'RandomString'文件夹(大约从10到100).
这是我尝试过的:
var dirs = Directory.GetDirectories(Server.MapPath(".."))
.Where(s => !s.EndsWith("Admin") || !s.EndsWith("Templates"));
foreach (string dir in dirs)
{
lit.Text += Environment.NewLine + dir;
}
Run Code Online (Sandbox Code Playgroud)
这将返回完整的文件夹列表(如上所示),而不会过滤掉"管理员"和"模板".
有趣的是,如果我将LINQ .Where子句更改为包含而不是排除,则"管理"和"模板"可以正常工作,这意味着它只返回"管理员"和"模板"的路径.
.Where(s => s.EndsWith("Admin") || s.EndsWith("Templates"));
Run Code Online (Sandbox Code Playgroud)
如果LINQ不是解决方案,有没有办法使用GetDirectories SearchPattern过滤掉目录?
我使用 Visual Studio 2015 在 C# 中开发了一个应用程序,该应用程序通常将一些文件从一个目录(源)复制到另一个目录(目标)。我的问题是源路径是域中的另一台计算机。我希望能够访问该目录并获取我的文件、用户域、源计算机的用户名和密码。我已经看到了一些解决方案,但我无法了解他们如何访问另一台计算机。我曾经通过使用目录来获取我的文件。GetDirectories(路径),我现在使用它太深了,无法将其更改为平滑。感谢您帮助我解决我的问题,我现在确实被封锁了好几天。
string[] folder1;
string[] folder2;
folder1 = Directory.GetDirectories(path);
foreach (string fld1 in folder1)
{
folder2 = Directory.GetDirectories(fld);
foreach(string fld2 in folder2)
{
for(int i = 0; i < MyList.Count(); i++)
{
if(fld2.Contains("nok") || fld2.Contains("Nok"))
LNok = Directory.GetFiles(fld2, picList[i]);
else
Lok = Directory.GetFiles(fld2, picList[i]);
}
}
}Run Code Online (Sandbox Code Playgroud)
在 C# 中,我想从与以下掩码匹配的特定目录中获取所有文件:
"myfile_" xmlIE
myfile_4.xml
myfile_24.xml
Run Code Online (Sandbox Code Playgroud)
以下文件不应与掩码匹配:
_myfile_6.xml
myfile_6.xml_
Run Code Online (Sandbox Code Playgroud)
代码应该像这样(也许一些 linq 查询可以提供帮助)
string[] files = Directory.GetFiles(folder, "???");
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试在桌面中创建一个文件夹(使用 DirectoryInfo) - 我需要获取桌面路径
我尝试过使用:
DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Run Code Online (Sandbox Code Playgroud)
但它不断让我进入用户的文件夹(桌面、音乐、视频文件夹所在的位置)。
DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "Folder111" );
dir.Create();
Run Code Online (Sandbox Code Playgroud) getdirectories ×11
c# ×8
.net ×2
directory ×2
linq ×2
asp.net ×1
attributes ×1
drive ×1
file-io ×1
path ×1
perl ×1
readdir ×1
subdirectory ×1