当前位置: 首页>>代码示例>>C#>>正文


C# DirectoryInfo.GetFileSystemInfos方法代码示例

本文整理汇总了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++;
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:82,代码来源:DirectoryInfo.GetFileSystemInfos

示例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++;
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:90,代码来源:DirectoryInfo.GetFileSystemInfos


注:本文中的System.IO.DirectoryInfo.GetFileSystemInfos方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。