本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

Directory.Exists 方法 (String)

 

确定给定路径是否引用磁盘上的现有目录。

命名空间:   System.IO
程序集:  mscorlib(位于 mscorlib.dll)

public static bool Exists(
	string path
)

参数

path
Type: System.String

要测试的路径。

返回值

Type: System.Boolean

如果 path 指向现有目录,则为 true;如果该目录不存在或者在尝试确定指定目录是否存在时出错,则为 false

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

Trailing spaces are removed from the end of the path parameter before checking whether the directory exists.

The path parameter is not case-sensitive.

If you do not have at a minimum read-only permission to the directory, the M:System.IO.Directory.Exists(System.String) method will return false.

The M:System.IO.Directory.Exists(System.String) method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.

// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}

FileIOPermission

for reading files. Associated enumeration: F:System.Security.Permissions.FileIOPermissionAccess.Read

通用 Windows 平台
自 10 起可用
.NET Framework
自 1.1 起可用
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
返回页首
显示: