Udostępnij za pośrednictwem


Directory.SetCurrentDirectory(String) Metoda

Definicja

Ustawia bieżący katalog roboczy aplikacji na określony katalog.

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)

Parametry

path
String

Ścieżka, do której ustawiono bieżący katalog roboczy.

Wyjątki

Wystąpił błąd we/wy.

Wersje .NET Framework i .NET Core starsze niż 2.1: path to ciąg o zerowej długości, zawiera tylko biały znak lub zawiera jeden lub więcej nieprawidłowych znaków. Możesz wykonać zapytanie o nieprawidłowe znaki za pomocą GetInvalidPathChars() metody .

Parametr path ma wartość null.

Określona ścieżka, nazwa pliku lub obie metody przekraczają maksymalną długość zdefiniowaną przez system.

Obiekt wywołujący nie ma wymaganych uprawnień dostępu do niezarządzanych kodów.

Nie można odnaleźć określonej ścieżki.

Nie można odnaleźć określonego katalogu.

Przykłady

W poniższym przykładzie pokazano, jak ustawić bieżący katalog i wyświetlić katalog główny katalogu.

// 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

Uwagi

Po zakończeniu działania aplikacji katalog roboczy zostanie przywrócony do oryginalnej lokalizacji (katalogu, w którym został uruchomiony proces).

Parametr path może określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względem bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, zobacz GetCurrentDirectory.

Spacje końcowe są usuwane z końca parametru path przed ustawieniem katalogu.

Wielkość liter parametru path odpowiada ważności liter systemu plików, na którym jest uruchomiony kod. Na przykład nie jest rozróżniana wielkość liter w systemie plików NTFS (domyślny system plików systemu Windows) i uwzględnia wielkość liter w systemach plików systemu Linux.

Jeśli ustawiasz katalog na dysk z nośnikami wymiennymi (na przykład "E:" dla dysku flash USB), możesz określić, czy dysk jest gotowy przy użyciu IsReady właściwości .

Dotyczy

Zobacz też