本文档已存档,并且将不进行维护。

FileInfo.Directory 属性

获取父目录的实例。

命名空间:System.IO
程序集:mscorlib(在 mscorlib.dll 中)

public DirectoryInfo Directory { get; }
/** @property */
public DirectoryInfo get_Directory ()

public function get Directory () : DirectoryInfo

属性值

表示此文件父目录的 DirectoryInfo 对象。

异常类型条件

DirectoryNotFoundException

指定的路径无效,比如在未映射的驱动器上。

SecurityException

调用方没有所要求的权限。

若要获取字符串形式的父目录,请使用 DirectoryName 属性。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

复制目录。

Directory

重命名或移动目录。

Directory.Move

DirectoryInfo.MoveTo

删除目录。

Directory.Delete

DirectoryInfo.Delete

创建目录。

CreateDirectory

Directory

创建子目录。

CreateSubdirectory

查看目录中的文件。

Name

查看目录的子目录。

GetDirectories

GetDirectories

查看目录的所有子目录中的所有文件。

GetFileSystemInfos

查看目录大小。

Directory

确定文件是否存在。

Exists

确定目录是否存在。

Exists

下面的示例打开或创建一个文件,确定其完整路径,确定并显示目录的所有内容。

using System;
using System.IO;

public class DirectoryTest 
{
    public static void Main() 
    {

        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        DirectoryInfo di = fi.Directory;

        // Figure out what other entries are in that directory.
        FileSystemInfo[] fsi = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        foreach (FileSystemInfo info in fsi)
            Console.WriteLine(info.Name);
    }
}

import System.*;
import System.IO.*;

public class DirectoryTest
{
    public static void main(String[] args)
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        DirectoryInfo di = fi.get_Directory();

        // Figure out what other entries are in that directory.
        FileSystemInfo fsi[] = di.GetFileSystemInfos();
        Console.WriteLine("The directory '{0}' contains the following files " 
            + " and directories:", di.get_FullName());

        // Print the names of all the files and subdirectories of
        // that directory.
        for (int iCtr = 0; iCtr < fsi.length; iCtr++) {
            FileSystemInfo info = (FileSystemInfo)fsi.get_Item(iCtr);
            Console.WriteLine(info.get_Name());
        }
    } //main
} //DirectoryTest

import System;
import System.IO;

public class DirectoryTest {
    public static function Main() : void {

        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        var di : DirectoryInfo = fi.Directory;

        // Figure out what other entries are in that directory.
        var fsi : FileSystemInfo[] = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        for (var i : int in fsi)
            Console.WriteLine(fsi[i].Name);
    }
}
DirectoryTest.Main();

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0
显示: