方法: ファイル、フォルダー、およびドライブに関する情報を取得する (C# プログラミング ガイド)
.NET Framework では、次のクラスを使用してファイル システム情報にアクセスできます。
FileInfo クラスおよび DirectoryInfo クラスはファイルまたはディレクトリを表し、NTFS ファイル システムでサポートされる多数のファイル属性を公開するプロパティを含みます。 ファイルやフォルダーを作成、クローズ、移動、および削除するためのメソッドも含まれます。 ファイル、フォルダー、またはドライブの名前を表す文字列をコンストラクターに渡すことで、これらのクラスのインスタンスを作成できます。
System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\");
DirectoryInfo.GetDirectories、DirectoryInfo.GetFiles、および DriveInfo.RootDirectory への呼び出しを使用することで、ファイル、フォルダー、またはドライブの名前を取得することもできます。
System.IO.Directory クラスと System.IO.File クラスは、ディレクトリおよびファイルに関する情報を取得するための静的メソッドを提供します。
使用例
ファイルおよびフォルダーに関する情報へのさまざまなアクセス方法を次の例に示します。
class FileSysInfo
{
static void Main()
{
// You can also use System.Environment.GetLogicalDrives to
// obtain names of all logical drives on the computer.
System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\");
Console.WriteLine(di.TotalFreeSpace);
Console.WriteLine(di.VolumeLabel);
// Get the root directory and print out some information about it.
System.IO.DirectoryInfo dirInfo = di.RootDirectory;
Console.WriteLine(dirInfo.Attributes.ToString());
// Get the files in the directory and print out some information about them.
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
foreach (System.IO.FileInfo fi in fileNames)
{
Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
// Get the subdirectories directly that is under the root.
// See "How to: Iterate Through a Directory Tree" for an example of how to
// iterate through an entire tree.
System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");
foreach (System.IO.DirectoryInfo d in dirInfos)
{
Console.WriteLine(d.Name);
}
// The Directory and File classes provide several static methods
// for accessing files and directories.
// Get the current application directory.
string currentDirName = System.IO.Directory.GetCurrentDirectory();
Console.WriteLine(currentDirName);
// Get an array of file names as strings rather than FileInfo objects.
// Use this method when storage space is an issue, and when you might
// hold on to the file name reference for a while before you try to access
// the file.
string[] files = System.IO.Directory.GetFiles(currentDirName, "*.txt");
foreach (string s in files)
{
// Create the FileInfo object only when needed to ensure
// the information is as current as possible.
System.IO.FileInfo fi = null;
try
{
fi = new System.IO.FileInfo(s);
}
catch (System.IO.FileNotFoundException e)
{
// To inform the user and continue is
// sufficient for this demonstration.
// Your application may require different behavior.
Console.WriteLine(e.Message);
continue;
}
Console.WriteLine("{0} : {1}",fi.Name, fi.Directory);
}
// Change the directory. In this case, first check to see
// whether it already exists, and create it if it does not.
// If this is not appropriate for your application, you can
// handle the System.IO.IOException that will be raised if the
// directory cannot be found.
if (!System.IO.Directory.Exists(@"C:\Users\Public\TestFolder\"))
{
System.IO.Directory.CreateDirectory(@"C:\Users\Public\TestFolder\");
}
System.IO.Directory.SetCurrentDirectory(@"C:\Users\Public\TestFolder\");
currentDirName = System.IO.Directory.GetCurrentDirectory();
Console.WriteLine(currentDirName);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
信頼性の高いプログラミング
ユーザー指定のパス文字列を処理する場合、次の条件の例外も処理する必要があります。
ファイル名が不適切である場合。 たとえば、無効な文字が含まれている場合や、空白のみの場合。
ファイル名が null である場合。
ファイル名の長さが、システム定義の最大長を超えている場合
ファイル名にコロン (:) が含まれる場合
指定したファイルの読み取りに必要なアクセス許可がアプリケーションに与えられていない場合、Exists メソッドは目的のパスが存在するかどうかに関係なく false を返します。ただし、例外はスローされません。