當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。