Directory クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
解説
Directory クラスは、ディレクトリのコピー、移動、名前変更、作成、削除などの一般的な操作に使用します。Directory クラスは、ディレクトリに対する作成、アクセス、および書き込み操作についての DateTime 情報の取得および設定にも使用できます。
Directory のメソッドはすべて静的であるため、1 つの操作を実行するだけであれば、Directory のメソッドを使用する方が、対応する DirectoryInfo のインスタンス メソッドを使用するよりも効率的な場合があります。Directory のメソッドのほとんどは、操作対象のディレクトリのパスを必要とします。
Directory クラスの静的メソッドは、すべてのメソッドでセキュリティ チェックを実行します。オブジェクトを何回か再利用する場合は、このようなセキュリティ チェックが必ずしも必要ではなくなるため、これらの静的メソッドの代わりに DirectoryInfo の対応するインスタンス メソッドを使用することを検討してください。
メモ |
---|
入力文字列としてパスを受け入れるメンバでは、そのパスが正しい書式である必要があります。それ以外の場合は、例外が発生します。たとえば、パスが絶対パスであっても空白で始まっている場合、そのパスはクラスのメソッドではトリムされません。このため、パスが正しい書式にならず、例外が発生します。同様に、1 つのパスまたは複数のパスの組み合わせを絶対パスとして 2 度指定することはできません。たとえば、"c:\temp c:\windows" でも、ほとんどの場合において例外が発生します。パス文字列を受け入れるメソッドを使用するときは、パスが適切な書式であることを確認します。 |
パスを受け入れるメンバでは、ファイルまたはディレクトリを参照するパスを指定できます。指定するパスは、相対パス、またはサーバーおよび共有名を示す UNC (Universal Naming Convention) パスにすることができます。たとえば、次に示すパスはすべて有効なパスです。
-
C# では "c:\\MyDir"、Visual Basic では "c:\MyDir"。
-
C# では "MyDir\\MySubdir"、Visual Basic では "MyDir\MySubDir"。
-
C# では "\\\\MyServer\\MyShare"、Visual Basic では "\\MyServer\MyShare"。
既定では、すべてのユーザーに、新しいディレクトリに対する完全な読み書きアクセス権が与えられます。パス文字列がディレクトリの区切り記号で終わるディレクトリに対するアクセス許可を要求すると、結果として、そのディレクトリに含まれるすべてのサブディレクトリに対するアクセス許可を要求することになります ("C:\Temp\" など)。特定のディレクトリに対するアクセス許可のみが必要な場合は、文字列を "." 文字で終わらせる必要があります ("C:\Temp\." など)。
その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
Directory.Move DirectoryInfo.MoveTo | |
Directory.Delete DirectoryInfo.Delete | |
CreateDirectory Directory | |
CreateSubdirectory | |
GetDirectories GetDirectories | |
GetFileSystemInfos | |
Directory | |
GetFileSystemInfos | |
指定したディレクトリが存在するかどうかを判断して、存在する場合はそれを削除し、存在しない場合はそれを作成するコード例を次に示します。この例では、この後にディレクトリを移動し、ディレクトリ内にファイルを作成し、ディレクトリ内のファイルの数をカウントします。
Imports System Imports System.IO Public Class Test Public Shared Sub Main() 'Specify the directories you want to manipulate. Dim path As String = "c:\MyDir" Dim target As String = "c:\TestDir" Try ' Determine whethers the directory exists. If Directory.Exists(path) = False Then ' Create the directory. Directory.CreateDirectory(path) End If If Directory.Exists(target) Then ' Delete the target to ensure it is not there. Directory.Delete(target, True) End If ' Move the directory. Directory.Move(path, target) 'Create a file in the directory. File.CreateText(target + "\myfile.txt") 'Count the files in the target. Console.WriteLine("The number of files in {0} is {1}", _ target, Directory.GetFiles(target).Length) Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class
using System; using System.IO; class Test { public static void Main() { // Specify the directories you want to manipulate. string path = @"c:\MyDir"; string target = @"c:\TestDir"; try { // Determine whether the directory exists. if (!Directory.Exists(path)) { // Create the directory it does not exist. Directory.CreateDirectory(path); } if (Directory.Exists(target)) { // Delete the target to ensure it is not there. Directory.Delete(target, true); } // Move the directory. Directory.Move(path, target); // Create a file in the directory. File.CreateText(target + @"\myfile.txt"); // Count the files in the target directory. Console.WriteLine("The number of files in {0} is {1}", target, Directory.GetFiles(target).Length); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally {} } }
using namespace System; using namespace System::IO; int main() { // Specify the directories you want to manipulate. String^ path = "c:\\MyDir"; String^ target = "c:\\TestDir"; try { // Determine whether the directory exists. if ( !Directory::Exists( path ) ) { // Create the directory it does not exist. Directory::CreateDirectory( path ); } if ( Directory::Exists( target ) ) { // Delete the target to ensure it is not there. Directory::Delete( target, true ); } // Move the directory. Directory::Move( path, target ); // Create a file in the directory. File::CreateText( String::Concat( target, "\\myfile.txt" ) ); // Count the files in the target directory. Console::WriteLine( "The number of files in {0} is {1}", target, Directory::GetFiles( target )->Length ); } catch ( Exception^ e ) { Console::WriteLine( "The process failed: {0}", e ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { // Specify the directories you want to manipulate. String path = "c:\\MyDir"; String target = "c:\\TestDir"; try { // Determine whether the directory exists. if (!(Directory.Exists(path))) { // Create the directory it does not exist. Directory.CreateDirectory(path); } if (Directory.Exists(target)) { // Delete the target to ensure it is not there. Directory.Delete(target, true); } // Move the directory. Directory.Move(path, target); // Create a file in the directory. File.CreateText(target + "\\myfile.txt"); // Count the files in the target directory. Console.WriteLine("The number of files in {0} is {1}", target, (Int32)Directory.GetFiles(target).length); } catch (System.Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally { } } //main } //Test
ディレクトリのサイズを計算する方法のコード例を次に示します。
' The following example calculates the size of a directory ' and its subdirectories, if any, and displays the total size ' in bytes. Imports System Imports System.IO Imports Microsoft.VisualBasic Public Class ShowDirSize Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long Dim Size As Long = 0 ' Add file sizes. Dim fis As FileInfo() = d.GetFiles() Dim fi As FileInfo For Each fi In fis Size += fi.Length Next fi ' Add subdirectory sizes. Dim dis As DirectoryInfo() = d.GetDirectories() Dim di As DirectoryInfo For Each di In dis Size += DirSize(di) Next di Return Size End Function 'DirSize Public Overloads Shared Sub Main(ByVal args() As String) If args.Length <> 1 Then Console.WriteLine("You must provide a directory argument at the command line.") Else Dim d As New DirectoryInfo(args(0)) Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d)) End If End Sub 'Main End Class 'ShowDirSize
// The following example calculates the size of a directory // and its subdirectories, if any, and displays the total size // in bytes. using System; using System.IO; public class ShowDirSize { public static long DirSize(DirectoryInfo d) { long Size = 0; // Add file sizes. FileInfo[] fis = d.GetFiles(); foreach (FileInfo fi in fis) { Size += fi.Length; } // Add subdirectory sizes. DirectoryInfo[] dis = d.GetDirectories(); foreach (DirectoryInfo di in dis) { Size += DirSize(di); } return(Size); } public static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("You must provide a directory argument at the command line."); } else { DirectoryInfo d = new DirectoryInfo(args[0]); Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d)); } } }
// The following example calculates the size of a directory // and its subdirectories, if any, and displays the total size // in bytes. using namespace System; using namespace System::IO; long DirSize( DirectoryInfo^ d ) { long Size = 0; // Add file sizes. array<FileInfo^>^fis = d->GetFiles(); System::Collections::IEnumerator^ myEnum = fis->GetEnumerator(); while ( myEnum->MoveNext() ) { FileInfo^ fi = safe_cast<FileInfo^>(myEnum->Current); Size += (long)fi->Length; } array<DirectoryInfo^>^dis = d->GetDirectories(); while ( myEnum->MoveNext() ) { DirectoryInfo^ di = safe_cast<DirectoryInfo^>(myEnum->Current); Size += DirSize( di ); } return Size; } int main() { array<String^>^args = Environment::GetCommandLineArgs(); if ( args->Length != 2 ) { Console::WriteLine( "You must provide a directory argument at the command line." ); } else { DirectoryInfo^ d = gcnew DirectoryInfo( args[ 1 ] ); Console::WriteLine( "The size of {0} and its subdirectories is {1} bytes.", d, DirSize( d ) ); } }
System.IO.Directory
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Directory メソッド
パブリック メソッド
Directory メンバ
ディレクトリやサブディレクトリを通じて、作成、移動、および列挙するための静的メソッドを公開します。このクラスは継承できません。
Directory データ型で公開されるメンバを以下の表に示します。
パブリック メソッド
Weblioに収録されているすべての辞書からdirectoryを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からdirectoryを検索
- directoryのページへのリンク