英語で読む

次の方法で共有


Environment.CurrentDirectory プロパティ

定義

現在の作業ディレクトリの完全修飾パスを取得または設定します。

C#
public static string CurrentDirectory { get; set; }

プロパティ値

ディレクトリのパスです。

例外

空の文字列 ("") に設定しようとしました。

null に設定しようとしました。

I/O エラーが発生しました。

見つからないローカル パスを設定しようとしました。

呼び出し元に、該当するアクセス許可がありません。

次の例では、 プロパティの設定を CurrentDirectory 示します。

C#
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.

注釈

定義上、このプロセスがローカル ドライブまたはネットワーク ドライブのルート ディレクトリで開始される場合、このプロパティの値はドライブ名の後に末尾のスラッシュ ("C:\" など) が続きます。 このプロセスがサブディレクトリで開始される場合、このプロパティの値は、末尾のスラッシュ ("C:\mySubDirectory" など) のないドライブとサブディレクトリのパスです。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1