System.IO.Directory.GetParent 方法

方法描述

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

语法定义(C# System.IO.Directory.GetParent 方法 的用法)

public static DirectoryInfo GetParent(
	string path
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 用于检索父目录的路径。
返回值 System.IO.DirectoryInfo 父目录;如果 path 是根目录,包括 UNC 服务器或共享名的根,则为 null。

提示和注释

该 path 参数可以指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参见 GetCurrentDirectory。

此方法返回的字符串由路径中最后的 DirectorySeparatorChar 或 AltDirectorySeparatorChar 之前(不包括该字符)的所有字符组成。 例如,将路径“C:\Directory\SubDirectory\test.txt”传入 GetParent 将返回“C:\Directory\SubDirectory”。 传入 "C:\Directory\SubDirectory" 返回 "C:\Directory"。 但是,传递“C:\Directory\SubDirectory\”将返回“C:\Directory\SubDirectory”,因为终止目录分隔符在“SubDirector”之后。

path 参数不区分大小写。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.Directory.GetParent 方法例子

将该示例配置为捕捉此方法的所有常见错误。

using System;

namespace GetFileSystemEntries
{
    class Class1 
    {
        static void Main(string[] args) 
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);		
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

		
        void PrintFileSystemEntries(string path) 
        {
			
            try 
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern) 
        {
            try 
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives() 
        {
            try 
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path) 
        {
            try 
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath) 
        {
            try 
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");	
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists."); 
            }
        }
    }
}

异常

异常 异常描述
IOException path 指定的目录是只读的。
UnauthorizedAccessException 调用方没有所要求的权限。
ArgumentException path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。
ArgumentNullException path 为 null。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。
DirectoryNotFoundException 未找到指定的路径。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。