Directory.GetLogicalDrives Method
Assembly: mscorlib (in mscorlib.dll)
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Rename or move a directory. | |
| Delete a directory. | |
| Create a directory. | |
| Create a subdirectory. | |
| See the files in a directory. | |
| See the subdirectories of a directory. | |
| See all the files in all subdirectories of a directory. | |
| Find the size of a directory. | |
| Determine if a file exists. | |
| Sort files in a directory by size. | GetFileSystemInfos |
| Determine if a directory exists. |
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."); } } } }
package GetFileSystemEntries ;
import System.*;
class Class1
{
public 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");
} //main
void PrintFileSystemEntries(String path)
{
try {
// Obtain the file system entries in the directory path.
String directoryEntries[] =
System.IO.Directory.GetFileSystemEntries(path);
for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
String str = directoryEntries[iCtr];
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException exp) {
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException exp) {
System.Console.WriteLine(("The caller does not have the "
+ "required permission."));
}
catch (ArgumentException exp) {
System.Console.WriteLine(("Path is an empty string, "
+ "contains only white spaces, "
+ "or contains invalid characters."));
}
catch (System.IO.DirectoryNotFoundException exp) {
System.Console.WriteLine(("The path encapsulated in the "
+ "Directory object does not exist."));
}
} //PrintFileSystemEntries
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);
for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
String str = directoryEntries[iCtr];
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException exp) {
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException exp) {
System.Console.WriteLine(("The caller does not have the "
+ "required permission."));
}
catch (ArgumentException exp) {
System.Console.WriteLine(("Path is an empty string, "
+ "contains only white spaces, "
+ "or contains invalid characters."));
}
catch (System.IO.DirectoryNotFoundException exp) {
System.Console.WriteLine(("The path encapsulated in the "
+ "Directory object does not exist."));
}
} //PrintFileSystemEntries
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try {
String drives[] = System.IO.Directory.GetLogicalDrives();
for (int iCtr = 0; iCtr < drives.length; iCtr++) {
String str = drives[iCtr];
System.Console.WriteLine(str);
}
}
catch (System.IO.IOException exp) {
System.Console.WriteLine("An I/O error occurs.");
}
catch (System.Security.SecurityException exp) {
System.Console.WriteLine(("The caller does not have the "
+ "required permission."));
}
} //GetLogicalDrives
void GetParent(String path)
{
try {
System.IO.DirectoryInfo directoryInfo =
System.IO.Directory.GetParent(path);
System.Console.WriteLine(directoryInfo.get_FullName());
}
catch (ArgumentNullException exp) {
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException exp) {
System.Console.WriteLine(("Path is an empty string, "
+"contains only white spaces, or contains invalid characters."));
}
} //GetParent
void Move(String sourcePath, String destinationPath)
{
try {
System.IO.Directory.Move(sourcePath, destinationPath);
System.Console.WriteLine("The directory move is complete.");
}
catch (ArgumentNullException exp) {
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException exp) {
System.Console.WriteLine(("The caller does not have the "
+ "required permission."));
}
catch (ArgumentException exp) {
System.Console.WriteLine(("Path is an empty string, "
+"contains only white spaces, or contains invalid characters."));
}
catch (System.IO.IOException exp) {
System.Console.WriteLine(("An attempt was made to move a "
+ "directory to a different "
+ "volume, or destDirName "
+ "already exists."));
}
} //Move
} //Class1
- SecurityPermission for calling unmanaged code, such as calling native code with PInvoke or COM interop. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.