Sdílet prostřednictvím


Directory.SetCurrentDirectory(String) Metoda

Definice

Nastaví aktuální pracovní adresář aplikace na zadaný adresář.

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

Cesta, ke které je nastaven aktuální pracovní adresář.

Výjimky

Došlo k vstupně-výstupní chybě.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody můžete zadat dotaz na neplatné znaky GetInvalidPathChars() .

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Volající nemá požadovaná oprávnění pro přístup k nespravovanému kódu.

Zadaná cesta nebyla nalezena.

Zadaný adresář nebyl nalezen.

Příklady

Následující příklad ukazuje, jak nastavit aktuální adresář a zobrazit kořen adresáře.

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

Poznámky

Po ukončení aplikace se pracovní adresář obnoví do původního umístění (adresář, ve kterém byl proces spuštěn).

Parametr path má povoleno zadat relativní nebo absolutní informace o cestě. Relativní informace o cestě se interpretují jako relativní vzhledem k aktuálnímu pracovnímu adresáři. Aktuální pracovní adresář získáte v tématu GetCurrentDirectory.

Koncové mezery se před nastavením adresáře odeberou z konce parametru path .

Citlivost na malá a velká písmena path parametru odpovídá systému souborů, na kterém je kód spuštěný. Například nerozlišuje malá a velká písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech linuxových souborů rozlišují malá a velká písmena.

Pokud nastavujete adresář na jednotku s vyměnitelným médium (například "E:" pro USB flash disk), můžete určit, jestli je jednotka připravená pomocí IsReady vlastnosti.

Platí pro

Viz také