Directory.SetCurrentDirectory メソッド
アセンブリ: mscorlib (mscorlib.dll 内)


例外の種類 | 条件 |
---|---|
IOException | |
ArgumentException | path が、長さが 0 の文字列であるか、空白しか含んでいないか、または InvalidPathChars で定義されている無効な文字を 1 つ以上含んでいます。 |
ArgumentNullException | path が null 参照 (Visual Basic では Nothing) です。 |
PathTooLongException | 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。 |
SecurityException | |
FileNotFoundException |

アプリケーションが終了すると、作業ディレクトリは元の位置 (プロセスが開始されたディレクトリ) に戻ります。
path パラメータは、相対パス情報または絶対パス情報を指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリを取得するには、GetCurrentDirectory のトピックを参照してください。
その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
Directory.Move DirectoryInfo.MoveTo | |
Directory.Delete DirectoryInfo.Delete | |
CreateDirectory | |
CreateSubdirectory |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows 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


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDirectory.SetCurrentDirectory メソッドを検索する場合は、下記のリンクをクリックしてください。

- Directory.SetCurrentDirectory メソッドのページへのリンク