本文整理汇总了C#中System.IO.DirectoryInfo.GetFileSystemInfos方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.GetFileSystemInfos方法的具体用法?C# DirectoryInfo.GetFileSystemInfos怎么用?C# DirectoryInfo.GetFileSystemInfos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.GetFileSystemInfos方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
class DirectoryFileCount
{
static long files = 0;
static long directories = 0;
static void Main()
{
try
{
Console.WriteLine("Enter the path to a directory:");
string directory = Console.ReadLine();
// Create a new DirectoryInfo object.
DirectoryInfo dir = new DirectoryInfo(directory);
if (!dir.Exists)
{
throw new DirectoryNotFoundException("The directory does not exist.");
}
// Call the GetFileSystemInfos method.
FileSystemInfo[] infos = dir.GetFileSystemInfos();
Console.WriteLine("Working...");
// Pass the result to the ListDirectoriesAndFiles
// method defined below.
ListDirectoriesAndFiles(infos);
// Display the results to the console.
Console.WriteLine("Directories: {0}", directories);
Console.WriteLine("Files: {0}", files);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadLine();
}
}
static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
{
// Check the FSInfo parameter.
if (FSInfo == null)
{
throw new ArgumentNullException("FSInfo");
}
// Iterate through each item.
foreach (FileSystemInfo i in FSInfo)
{
// Check to see if this is a DirectoryInfo object.
if (i is DirectoryInfo)
{
// Add one to the directory count.
directories++;
// Cast the object to a DirectoryInfo object.
DirectoryInfo dInfo = (DirectoryInfo)i;
// Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());
}
// Check to see if this is a FileInfo object.
else if (i is FileInfo)
{
// Add one to the file count.
files++;
}
}
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
class DirectoryFileCount
{
static long files = 0;
static long directories = 0;
static void Main()
{
try
{
Console.WriteLine("Enter the path to a directory:");
string directory = Console.ReadLine();
Console.WriteLine("Enter a search string (for example *p*):");
string searchString = Console.ReadLine();
// Create a new DirectoryInfo object.
DirectoryInfo dir = new DirectoryInfo(directory);
if (!dir.Exists)
{
throw new DirectoryNotFoundException("The directory does not exist.");
}
// Call the GetFileSystemInfos method.
FileSystemInfo[] infos = dir.GetFileSystemInfos(searchString);
Console.WriteLine("Working...");
// Pass the result to the ListDirectoriesAndFiles
// method defined below.
ListDirectoriesAndFiles(infos, searchString);
// Display the results to the console.
Console.WriteLine("Directories: {0}", directories);
Console.WriteLine("Files: {0}", files);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadLine();
}
}
static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo, string SearchString)
{
// Check the parameters.
if (FSInfo == null)
{
throw new ArgumentNullException("FSInfo");
}
if (SearchString == null || SearchString.Length == 0)
{
throw new ArgumentNullException("SearchString");
}
// Iterate through each item.
foreach (FileSystemInfo i in FSInfo)
{
// Check to see if this is a DirectoryInfo object.
if (i is DirectoryInfo)
{
// Add one to the directory count.
directories++;
// Cast the object to a DirectoryInfo object.
DirectoryInfo dInfo = (DirectoryInfo)i;
// Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString), SearchString);
}
// Check to see if this is a FileInfo object.
else if (i is FileInfo)
{
// Add one to the file count.
files++;
}
}
}
}