Environment.CurrentDirectory 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定目前工作目錄的完整路徑。
public:
static property System::String ^ CurrentDirectory { System::String ^ get(); void set(System::String ^ value); };
public static string CurrentDirectory { get; set; }
static member CurrentDirectory : string with get, set
Public Shared Property CurrentDirectory As String
屬性值
目錄路徑。
例外狀況
嘗試設定為空字串 ("")。
嘗試設定為 null
。
發生 I/O 錯誤。
嘗試設定找不到的本機路徑。
呼叫端沒有適當的權限。
範例
下列範例示範如何設定 CurrentDirectory 屬性。
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.
open System
open System.IO
if Environment.OSVersion.Platform = PlatformID.Win32NT then
// Change the directory to %WINDIR%
Environment.CurrentDirectory <- Environment.GetEnvironmentVariable "windir"
let info = DirectoryInfo "."
printfn $"Directory Info: {info.FullName}"
else
printfn "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:\」 ) 。 如果這個程式在子目錄中啟動,則此屬性的值是磁片磁碟機和子目錄路徑,而沒有尾端斜線 (,例如 「C:\mySubDirectory」) 。