Environment.CurrentDirectory 属性

定义

获取或设置当前工作目录的完全限定路径。

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”) 。

适用于