Directory.SetCurrentDirectory(String) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee stelt u de huidige werkmap van de toepassing in op de opgegeven map.
public:
static void SetCurrentDirectory(System::String ^ path);
public static void SetCurrentDirectory(string path);
static member SetCurrentDirectory : string -> unit
Public Shared Sub SetCurrentDirectory (path As String)
Parameters
- path
- String
Het pad waarnaar de huidige werkmap is ingesteld.
Uitzonderingen
Er is een I/O-fout opgetreden.
.NET Framework en .NET Core-versies ouder dan 2.1: path is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met de GetInvalidPathChars() methode.
path is null.
Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.
De aanroeper beschikt niet over de vereiste machtigingen voor toegang tot niet-beheerde code.
Het opgegeven pad is niet gevonden.
De opgegeven map is niet gevonden.
Voorbeelden
In het volgende voorbeeld ziet u hoe u de huidige map instelt en de hoofdmap van de map weergeeft.
// This sample shows how to set the current directory and how to determine
// the root directory.
using System;
using System.IO;
namespace IOSamples
{
public class DirectoryRoot
{
public static void Main()
{
// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
string dir = @"C:\test";
try
{
//Set the current directory.
Directory.SetCurrentDirectory(dir);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("The specified directory does not exist. {0}", e);
}
// Print to console the results.
Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir));
Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
}
}
}
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
open System.IO
// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
let dir = @"C:\test"
try
//Set the current directory.
Directory.SetCurrentDirectory dir
with :? DirectoryNotFoundException as e ->
printfn $"The specified directory does not exist. {e}"
// Print to console the results.
printfn $"Root directory: {Directory.GetDirectoryRoot dir}"
printfn $"Current directory: {Directory.GetCurrentDirectory()}"
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
' This sample shows how to set the current directory and how to determine
' the root directory.
Imports System.IO
Public Class DirectoryRoot
Public Shared Sub Main()
' Create string for a directory. This value should be an existing directory
' or the sample will throw a DirectoryNotFoundException.
Dim dir As String = "C:\test"
Try
'Set the current directory.
Directory.SetCurrentDirectory(dir)
Catch e As DirectoryNotFoundException
Console.WriteLine("The specified directory does not exist. {0}", e)
End Try
' Print to console the results.
Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir))
Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory())
End Sub
End Class
' The output of this sample depends on what value you assign to the variable dir.
' If the directory c:\test exists, the output for this sample is:
' Root directory: C:\
' Current directory: C:\test
Opmerkingen
Wanneer de toepassing wordt beëindigd, wordt de werkmap hersteld naar de oorspronkelijke locatie (de map waar het proces is gestart).
De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Als u de huidige werkmap wilt ophalen, raadpleegt GetCurrentDirectoryu .
Volgspaties worden verwijderd uit het einde van de path parameter voordat u de map instelt.
De hoofdlettergevoeligheid van de path parameter komt overeen met die van het bestandssysteem waarop de code wordt uitgevoerd. Het is bijvoorbeeld niet hoofdlettergevoelig voor NTFS (het standaardbestandssysteem Windows) en hoofdlettergevoelig op Linux-bestandssystemen.
Als u de map instelt op een station met verwisselbare media (bijvoorbeeld 'E:' voor een USB-flashstation), kunt u bepalen of het station gereed is met behulp van de IsReady eigenschap.