System.IO.Directory.GetLogicalDrives 方法
方法描述
检索此计算机上格式为“<盘符>:\”的逻辑驱动器的名称。
语法定义(C# System.IO.Directory.GetLogicalDrives 方法 的用法)
public static string[] GetLogicalDrives()
参数/返回值
参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
---|---|---|
返回值 | System.String[] | 此计算机上的逻辑驱动器。 |
提示和注释
GetLogicalDrives 返回特定计算机中所有可访问的驱动器,包括软盘驱动器和任何光盘驱动器。
有关通用 I/O 任务的列表,请参见通用 I/O 任务。
System.IO.Directory.GetLogicalDrives 方法例子
将该示例配置为捕捉此方法的所有常见错误。
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 | 发生 I/O 错误(例如,磁盘错误)。 |
UnauthorizedAccessException | 调用方没有所要求的权限。 |
版本信息
.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 系统要求。