本文整理汇总了C#中System.IO.DirectoryInfo.GetDirectories方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.GetDirectories方法的具体用法?C# DirectoryInfo.GetDirectories怎么用?C# DirectoryInfo.GetDirectories使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.GetDirectories方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
public class GetDirectoriesTest
{
public static void Main()
{
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("c:\\");
// Get a reference to each directory in that directory.
DirectoryInfo[] diArr = di.GetDirectories();
// Display the names of the directories.
foreach (DirectoryInfo dri in diArr)
Console.WriteLine(dri.Name);
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");
// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");
Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length);
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} is {1}", diNext,
diNext.GetFiles().Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
示例3: Main
//引入命名空间
using System;
using System.IO;
class App
{
public static void Main()
{
// Specify the directory you want to manipulate.
string path = @"c:\";
string searchPattern = "c*";
DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories =
di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
FileInfo[] files =
di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
Console.WriteLine(
"Directories that begin with the letter \"c\" in {0}", path);
foreach (DirectoryInfo dir in directories)
{
Console.WriteLine(
"{0,-25} {1,25}", dir.FullName, dir.LastWriteTime);
}
Console.WriteLine();
Console.WriteLine(
"Files that begin with the letter \"c\" in {0}", path);
foreach (FileInfo file in files)
{
Console.WriteLine(
"{0,-25} {1,25}", file.Name, file.LastWriteTime);
}
} // Main()
} // App()
示例4: DirectoryInfo.GetDirectories()
//引入命名空间
using System;
using System.IO;
public class DirectoryCounter {
public static void Main() {
DirectoryInfo myDir = new DirectoryInfo(@"c:\Program Files");
int total = CountDirectories(myDir);
Console.WriteLine(total);
}
public static int CountDirectories(DirectoryInfo dir) {
DirectoryInfo[] dirs = dir.GetDirectories();
int count = dirs.Length;
foreach (DirectoryInfo subDir in dirs) {
count += CountDirectories(subDir);
}
return count;
}
}