Environment.CurrentDirectory Environment.CurrentDirectory Environment.CurrentDirectory Environment.CurrentDirectory Property

定義

取得或設定目前工作目錄的完整路徑。Gets or sets the fully qualified path of the current working directory.

public:
 static property System::String ^ CurrentDirectory { System::String ^ get(); void set(System::String ^ value); };
public static string CurrentDirectory { get; set; }
member this.CurrentDirectory : string with get, set
Public Shared Property CurrentDirectory As String

屬性值

含有目錄路徑的字串。A string containing a directory path.

例外狀況

嘗試設定為空字串 ("")。Attempted to set to an empty string ("").

發生 I/O 錯誤。An I/O error occurred.

嘗試設定找不到的本機路徑。Attempted to set a local path that cannot be found.

呼叫端沒有適當的權限。The caller does not have the appropriate permission.

範例

下列範例示範如何設定CurrentDirectory屬性。The following example demonstrates setting the CurrentDirectory property.

using namespace System;
using namespace System::IO;

void main()
{
      if (Environment::OSVersion->Platform == PlatformID::Win32NT)
      {
            // Change the directory to %WINDIR%
            Environment::CurrentDirectory = Environment::GetEnvironmentVariable( "windir" );
            DirectoryInfo^ info = gcnew DirectoryInfo( "." );

            Console::WriteLine("Directory Info:   {0}", info->FullName);
      }
      else
      {
            Console::WriteLine("This example runs on Windows only.");
      }
}
// The example displays output like the following on a .NET implementation running on Windows:
//        Directory Info:   C:\windows
// The example displays the following output on a .NET implementation on Unix-based systems:
//        This example runs on Windows only.
using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      if (Environment.OSVersion.Platform == PlatformID.Win32NT)
      {    
         // Change the directory to %WINDIR%
         Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
         DirectoryInfo info = new DirectoryInfo(".");

         Console.WriteLine("Directory Info:   " + info.FullName);
      }
      else
      {
         Console.WriteLine("This example runs on Windows only.");
      }
   }
}
// The example displays output like the following on a .NET implementation running on Windows:
//        Directory Info:   C:\windows
// The example displays the following output on a .NET implementation on Unix-based systems:
//        This example runs on Windows only.
Imports System.IO

Module Example
   Public Sub Main()
        If Environment.OSVersion.Platform = PlatformID.Win32NT Then
            ' Change the directory to %WINDIR%
            Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir")
            Dim info As New DirectoryInfo(".")
            Console.WriteLine("Directory Info:   " + info.FullName)
         Else
            Console.WriteLine("This example runs on Windows only.")
         End If
   End Sub
End Module
' The example displays output like the following on a .NET implementation running on Windows:
'        Directory Info:   C:\windows
' The example displays the following output on a .NET implementation on Unix-based systems:
'        This example runs on Windows only.

備註

根據定義, 如果此進程是在本機或網路磁碟機機的根目錄中啟動, 此屬性的值就是磁片磁碟機名稱, 後面接著尾端斜線 (例如 "C:\")。By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:\"). 如果這個進程是在子目錄中啟動, 此屬性的值會是磁片磁碟機和子目錄路徑, 不含尾端斜線 (例如 "C:\mySubDirectory")。If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").

安全性

SecurityPermission
用於寫入設定作業中的檔案或目錄。for writing to files or directories in a set operation. 相關聯的列舉:UnmanagedCodeAssociated enumeration: UnmanagedCode

FileIOPermission
用於存取 get 作業中路徑本身的資訊。for access to the information in the path itself in a get operation. 相關聯的列舉:PathDiscoveryAssociated enumeration: PathDiscovery

適用於