Directory.SetCurrentDirectory(String) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Anger programmets aktuella arbetskatalog till den angivna katalogen.
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)
Parametrar
- path
- String
Sökvägen till vilken den aktuella arbetskatalogen har angetts.
Undantag
Ett I/O-fel uppstod.
.NET Framework- och .NET Core-versioner som är äldre än 2.1: path är en sträng med noll längd, innehåller endast tomt utrymme eller innehåller ett eller flera ogiltiga tecken. Du kan fråga efter ogiltiga tecken med GetInvalidPathChars() metoden.
path är null.
Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.
Anroparen har inte den behörighet som krävs för att få åtkomst till ohanterad kod.
Det gick inte att hitta den angivna sökvägen.
Det gick inte att hitta den angivna katalogen.
Exempel
I följande exempel visas hur du anger den aktuella katalogen och visar katalogroten.
// 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
Kommentarer
När programmet avslutas återställs arbetskatalogen till den ursprungliga platsen (katalogen där processen startades).
Parametern path tillåts ange relativ eller absolut sökvägsinformation. Relativ sökvägsinformation tolkas som relativ till den aktuella arbetskatalogen. Information om hur du hämtar den aktuella arbetskatalogen finns i GetCurrentDirectory.
Avslutande blanksteg tas bort från slutet av parametern path innan du ställer in katalogen.
Parameterns path skiftlägeskänslighet motsvarar det filsystem där koden körs. Det är till exempel skiftlägeskänsligt på NTFS (standard Windows filsystem) och skiftlägeskänsligt på Linux-filsystem.
Om du ställer in katalogen på en enhet med flyttbara medier (till exempel "E:" för ett USB-flashminne) kan du avgöra om enheten är klar med hjälp IsReady av egenskapen .