Directory.SetCurrentDirectory メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Directory.SetCurrentDirectory メソッドの意味・解説 

Directory.SetCurrentDirectory メソッド

アプリケーション現在の作業ディレクトリ指定したディレクトリ設定します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Sub SetCurrentDirectory
 ( _
    path As String _
)
Dim path As String

Directory.SetCurrentDirectory(path)
public static void SetCurrentDirectory
 (
    string path
)
public:
static void SetCurrentDirectory (
    String^ path
)
public static void SetCurrentDirectory
 (
    String path
)
public static function SetCurrentDirectory
 (
    path : String
)

パラメータ

path

現在の作業ディレクトリ設定されるパス

例外例外
例外種類条件

IOException

IO エラー発生しました

ArgumentException

path が、長さが 0 の文字列であるか、空白しか含んでいないか、または InvalidPathChars で定義されている無効な文字1 つ以上含んでます。

ArgumentNullException

pathnull 参照 (Visual Basic では Nothing) です。

PathTooLongException

指定したパスファイル名、またはその両方システム定義の最大長を超えてます。たとえば、Windows ベースプラットフォーム場合パス長さ248 文字未満ファイル名長さ260 文字未満である必要があります

SecurityException

アンマネージ コードアクセスするために必要なアクセス許可が、呼び出し元にありません。

FileNotFoundException

指定したパスが見つかりませんでした

解説解説

アプリケーション終了すると、作業ディレクトリは元の位置 (プロセス開始されディレクトリ) に戻ります

path パラメータは、相対パス情報または絶対パス情報指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリ取得するには、GetCurrentDirectory のトピック参照してください

path パラメータでは大文字と小文字区別されません。

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します

Windows Mobile for Pocket PCWindows Mobile for SmartphoneWindows CE プラットフォームメモ : 一部モバイル デバイスオペレーティング システムは "現在のディレクトリ" 機能備えていないため、このメソッドサポートされません。

使用例使用例

現在のディレクトリ設定およびディレクトリ ルート表示実行するコード例次に示します

' This sample shows how to set the current directory and how to determine
' the root directory.
Imports System
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 'Main
End Class 'DirectoryRoot
' 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.
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.
using namespace System;
using namespace System::IO;
int 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.

import System.*;
import System.IO.*;

public class DirectoryRoot
{
    public static void main(String[]
 args)
    {
        // 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());
    } //main
} //DirectoryRoot
// 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
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からDirectory.SetCurrentDirectory メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDirectory.SetCurrentDirectory メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDirectory.SetCurrentDirectory メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Directory.SetCurrentDirectory メソッド」の関連用語

Directory.SetCurrentDirectory メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Directory.SetCurrentDirectory メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS