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

Directory 类

 

公开用于通过目录和子目录进行创建、移动和枚举的静态方法。 无法继承此类。

若要浏览此类型的 .NET Framework 源代码,请参阅引用源

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

System.Object
  System.IO.Directory

[ComVisibleAttribute(true)]
public static class Directory

名称说明
System_CAPS_pubmethodSystem_CAPS_staticCreateDirectory(String)

在指定路径中创建所有目录和子目录,除非它们已经存在。

System_CAPS_pubmethodSystem_CAPS_staticCreateDirectory(String, DirectorySecurity)

在指定路径中创建所有目录(除非已存在),并应用指定的 Windows 安全性。

System_CAPS_pubmethodSystem_CAPS_staticDelete(String)

从指定路径删除空目录。

System_CAPS_pubmethodSystem_CAPS_staticDelete(String, Boolean)

删除指定的目录,并删除该目录中的所有子目录和文件(如果表示)。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateDirectories(String)

返回指定路径中的目录名的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateDirectories(String, String)

返回指定路径中与搜索模式匹配的目录名的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateDirectories(String, String, SearchOption)

返回指定路径中与搜索模式匹配的目录名称的可枚举集合,还可以搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFiles(String)

返回指定路径中的文件名的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFiles(String, String)

返回指定路径中与搜索模式匹配的文件名称的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFiles(String, String, SearchOption)

返回指定路径中与搜索模式匹配的文件名称的可枚举集合,还可以搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFileSystemEntries(String)

返回指定路径中的文件名和目录名的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFileSystemEntries(String, String)

返回指定路径中与搜索模式匹配的文件名和目录名的可枚举集合。

System_CAPS_pubmethodSystem_CAPS_staticEnumerateFileSystemEntries(String, String, SearchOption)

返回指定路径中与搜索模式匹配的文件名称和目录名的可枚举集合,还可以搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticExists(String)

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

System_CAPS_pubmethodSystem_CAPS_staticGetAccessControl(String)

获取 DirectorySecurity 对象,该对象封装指定目录的访问控制列表 (ACL) 项。

System_CAPS_pubmethodSystem_CAPS_staticGetAccessControl(String, AccessControlSections)

获取一个 DirectorySecurity 对象,它封装指定目录的指定类型的访问控制列表 (ACL) 条目。

System_CAPS_pubmethodSystem_CAPS_staticGetCreationTime(String)

获取目录的创建日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticGetCreationTimeUtc(String)

获取目录创建的日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_pubmethodSystem_CAPS_staticGetCurrentDirectory()

获取应用程序的当前工作目录。

System_CAPS_pubmethodSystem_CAPS_staticGetDirectories(String)

返回指定目录中的子目录的名称(包括其路径)。

System_CAPS_pubmethodSystem_CAPS_staticGetDirectories(String, String)

返回指定目录中与指定的搜索模式匹配的子目录的名称(包括其路径)。

System_CAPS_pubmethodSystem_CAPS_staticGetDirectories(String, String, SearchOption)

返回与在指定目录中的指定搜索模式匹配的子目录的名称(包括其路径),还可以选择地搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticGetDirectoryRoot(String)

返回指定路径的卷信息、根信息或两者同时返回。

System_CAPS_pubmethodSystem_CAPS_staticGetFiles(String)

返回指定目录中文件的名称(包括其路径)。

System_CAPS_pubmethodSystem_CAPS_staticGetFiles(String, String)

返回指定目录中与指定的搜索模式匹配的文件的名称(包含其路径)。

System_CAPS_pubmethodSystem_CAPS_staticGetFiles(String, String, SearchOption)

返回指定目录中与指定的搜索模式匹配的文件的名称(包含其路径),使用某个值确定是否要搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticGetFileSystemEntries(String)

返回指定路径中的所有文件和子目录的名称。

System_CAPS_pubmethodSystem_CAPS_staticGetFileSystemEntries(String, String)

返回与指定路径中搜索模式匹配的文件名和目录名的数组。

System_CAPS_pubmethodSystem_CAPS_staticGetFileSystemEntries(String, String, SearchOption)

返回指定路径中与搜索模式匹配的所有文件名和目录名的数组,还可以搜索子目录。

System_CAPS_pubmethodSystem_CAPS_staticGetLastAccessTime(String)

返回上次访问指定文件或目录的日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticGetLastAccessTimeUtc(String)

返回上次访问指定文件或目录的日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_pubmethodSystem_CAPS_staticGetLastWriteTime(String)

返回上次写入指定文件或目录的日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticGetLastWriteTimeUtc(String)

返回上次写入指定文件或目录的日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_pubmethodSystem_CAPS_staticGetLogicalDrives()

检索此计算机上格式为 "<drive letter>:\" 的逻辑驱动器的名称。

System_CAPS_pubmethodSystem_CAPS_staticGetParent(String)

检索指定路径的父目录,包括绝对路径和相对路径。

System_CAPS_pubmethodSystem_CAPS_staticMove(String, String)

将文件或目录及其内容移到新位置。

System_CAPS_pubmethodSystem_CAPS_staticSetAccessControl(String, DirectorySecurity)

DirectorySecurity 对象描述的访问控制列表 (ACL) 项应用于指定的目录。

System_CAPS_pubmethodSystem_CAPS_staticSetCreationTime(String, DateTime)

为指定的文件或目录设置创建日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticSetCreationTimeUtc(String, DateTime)

设置指定文件或目录的创建日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_pubmethodSystem_CAPS_staticSetCurrentDirectory(String)

将应用程序的当前工作目录设置为指定的目录。

System_CAPS_pubmethodSystem_CAPS_staticSetLastAccessTime(String, DateTime)

设置上次访问指定文件或目录的日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticSetLastAccessTimeUtc(String, DateTime)

设置上次访问指定文件或目录的日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_pubmethodSystem_CAPS_staticSetLastWriteTime(String, DateTime)

设置上次写入目录的日期和时间。

System_CAPS_pubmethodSystem_CAPS_staticSetLastWriteTimeUtc(String, DateTime)

设置上次写入某个目录的日期和时间,其格式为协调通用时 (UTC)。

System_CAPS_note说明

To view the .NET Framework source code for this type, see the Reference Sourcehttp://referencesource.microsoft.com/#mscorlib/system/io/directory.cs#b3ad5f0ba800bb28. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructionshttp://referencesource.microsoft.com/.

Use the T:System.IO.Directory class for typical operations such as copying, moving, renaming, creating, and deleting directories.

  • To create a directory, use one of the M:System.IO.Directory.CreateDirectory(System.String) methods.

  • To delete a directory, use one of the M:System.IO.Directory.Delete(System.String) methods.

  • To get or set the current directory for an app, use the M:System.IO.Directory.GetCurrentDirectory or M:System.IO.Directory.SetCurrentDirectory(System.String) method.

  • To manipulate T:System.DateTime information related to the creation, access, and writing of a directory, use methods such as M:System.IO.Directory.SetLastAccessTime(System.String,System.DateTime) and M:System.IO.Directory.SetCreationTime(System.String,System.DateTime).

The static methods of the T:System.IO.Directory class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of T:System.IO.DirectoryInfo instead, because the security check will not always be necessary.

If you are performing only one directory-related action, it might be more efficient to use a static T:System.IO.Directory method rather than a corresponding T:System.IO.DirectoryInfo instance method. Most T:System.IO.Directory methods require the path to the directory that you are manipulating.

System_CAPS_note说明

In members that accept a string path parameter, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space (" c:\temp"), the path string isn't trimmed, so the path is considered malformed and an exception is raised. In addition, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception. Ensure that your paths are well-formed when using methods that accept a path string. For more information see T:System.IO.Path.

In members that accept a path, the path can refer to a file or a directory. You can use a full path, a relative path, or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

  • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

By default, full read/write access to new directories is granted to all users. However, the app must have the correct security to access existing directories.

To demand permissions for a directory and all its subdirectories, end the path string with the directory separator character. (For example, "C:\Temp\" grants access to C:\Temp\ and all its subdirectories.) To demand permissions only for a specific directory, end the path string with a period. (For example, "C:\Temp\." grants access only to C:\Temp\, not to its subdirectories.)

In members that accept a searchPattern parameter, the search string can be any combination of literal characters and two wildcard characters; * and ?. This parameter does not recognize regular expressions. For more information, see the M:System.IO.Directory.EnumerateDirectories(System.String,System.String) method or any other method that uses the searchPattern parameter.

For a list of common I/O tasks, see Common I/O Tasks.

Directory and DirectoryInfo are not supported for use in Windows 应用商店 apps. For information about how to access files and folders in Windows 应用商店 apps, see Accessing data and files (Windows Store apps).

The following example shows how to retrieve all the text files from a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\current";
            string archiveDirectory = @"C:\archive";

            try
            {
                var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");

                foreach (string currentFile in txtFiles)
                {
                    string fileName = currentFile.Substring(sourceDirectory.Length + 1);
                    Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

The following example demonstrates how to use the M:System.IO.Directory.EnumerateFiles(System.String) method to retrieve a collection of text files from a directory, and then use that collection in a query to find all the lines that contain "Example".

using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string archiveDirectory = @"C:\archive";

            var files = from retrievedFile in Directory.EnumerateFiles(archiveDirectory, "*.txt", SearchOption.AllDirectories)
                        from line in File.ReadLines(retrievedFile)
                        where line.Contains("Example")
                        select new
                        {
                            File = retrievedFile,
                            Line = line
                        };

            foreach (var f in files)
            {
                Console.WriteLine("{0} contains {1}", f.File, f.Line);
            }
            Console.WriteLine("{0} lines found.", files.Count().ToString());
        }
    }
}

The following example demonstrates how to move a directory and all its files to a new directory. The original directory no longer exists after it has been moved.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\source";
            string destinationDirectory = @"C:\destination";

            try
            {
                Directory.Move(sourceDirectory, destinationDirectory);  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

通用 Windows 平台
自 10 起可用
.NET Framework
自 1.1 起可用
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用

此类型的所有公共静态(Visual Basic 中的 已共享 在 Visual Basic 中)成员都是线程安全的。不保证所有实例成员都是线程安全的。

返回页首
显示: