FileInfo クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class FileInfo Inherits FileSystemInfo
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class FileInfo sealed : public FileSystemInfo
FileInfo クラスは、ファイルのコピー、移動、名前変更、作成、オープン、削除、内容の追加などの一般的な操作に使用します。
FileInfo のメソッドの多くは、ファイルを作成またはオープンしたときにそれぞれに異なる I/O 型を返します。これらの型は、以降のファイル操作に使用できます。詳細については、Open、OpenRead、OpenText、CreateText、Create などの具体的な FileInfo メンバのトピックを参照してください。
オブジェクトを何回か再利用する場合は、必ずしもセキュリティ チェックが必要ではなくなるため、File の対応する静的メソッドの代わりに FileInfo のインスタンス メソッドを使用することを検討してください。
既定では、すべてのユーザーに、新しいファイルに対する完全な読み書きアクセス権が与えられます。
さまざまな FileInfo メソッドの動作をカスタマイズするために使用する列挙体を次の表に示します。
列挙体 | |
---|---|
FileShare | |
FileMode |
メモ |
---|
入力文字列としてパスを受け入れるメンバでは、そのパスが正しい書式である必要があります。それ以外の場合は、例外が発生します。たとえば、パスが絶対パスであっても空白で始まっている場合、そのパスはクラスのメソッドではトリムされません。このため、パスが正しい書式にならず、例外が発生します。同様に、1 つのパスまたは複数のパスの組み合わせで、2 つの絶対パスを指定することはできません。たとえば、"c:\temp c:\windows" でも、ほとんどの場合において例外が発生します。パス文字列を受け入れるメソッドを使用するときは、パスが適切な書式であることを確認します。 |
パスを受け入れるメンバでは、ファイルまたはディレクトリを参照するパスを指定できます。指定するパスは、相対パス、またはサーバーおよび共有名を示す UNC (Universal Naming Convention) パスにすることができます。たとえば、次に示すパスはすべて有効なパスです。
-
C# では "c:\\MyDir\\MyFile.txt"、Visual Basic では "c:\MyDir\MyFile.txt"。
-
C# では "c:\\MyDir"、Visual Basic では "c:\MyDir"。
-
C# では "MyDir\\MySubdir"、Visual Basic では "MyDir\MySubDir"。
-
C# では "\\\\MyServer\\MyShare"、Visual Basic では "\\MyServer\MyShare"。
その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
File.AppendText FileInfo.AppendText | |
File.Move FileInfo.MoveTo | |
File.Delete FileInfo.Delete | |
File.Copy FileInfo.CopyTo | |
FileInfo.Length | |
File.GetAttributes | |
File.SetAttributes | |
Path.GetExtension | |
Path.GetFullPath | |
Path.GetFileName | |
Path.ChangeExtension |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : デバイスのファイル システムはぞれぞれ動作が異なるため、.NET Compact Framework ではファイル属性の取得または設定をサポートしていません。
Imports System Imports System.IO Public Class Test Public Shared Sub Main() Dim path1 As String = Path.GetTempFileName() Dim path2 As String = Path.GetTempFileName() Dim fi As FileInfo = New FileInfo(path1) If fi.Exists = False Then 'Create a file to write to. Dim sw As StreamWriter = fi.CreateText() sw.WriteLine("Hello") sw.WriteLine("And") sw.WriteLine("Welcome") sw.Flush() sw.Close() End If Try 'Open the file to read from. Dim sr As StreamReader = fi.OpenText() Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop sr.Close() Dim fi2 As FileInfo = New FileInfo(path2) 'Ensure that the target does not exist. fi2.Delete() 'Copy the file. fi.CopyTo(path2) Console.WriteLine("{0} was copied to {1}.", path1, path2) 'Delete the newly created file. fi2.Delete() Console.WriteLine("{0} was successfully deleted.", path2) 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() { string path = Path.GetTempFileName(); FileInfo fi1 = new FileInfo(path); if (!fi1.Exists) { //Create a file to write to. using (StreamWriter sw = fi1.CreateText()) { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } } //Open the file to read from. using (StreamReader sr = fi1.OpenText()) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } try { string path2 = Path.GetTempFileName(); FileInfo fi2 = new FileInfo(path2); //Ensure that the target does not exist. fi2.Delete(); //Copy the file. fi1.CopyTo(path2); Console.WriteLine("{0} was copied to {1}.", path, path2); //Delete the newly created file. fi2.Delete(); Console.WriteLine("{0} was successfully deleted.", path2); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
using namespace System; using namespace System::IO; int main() { String^ path = Path::GetTempFileName(); FileInfo^ fi1 = gcnew FileInfo( path ); if ( !fi1->Exists ) { //Create a file to write to. StreamWriter^ sw = fi1->CreateText(); try { sw->WriteLine( "Hello" ); sw->WriteLine( "And" ); sw->WriteLine( "Welcome" ); } finally { if ( sw ) delete (IDisposable^)sw; } } //Open the file to read from. StreamReader^ sr = fi1->OpenText(); try { String^ s = ""; while ( s = sr->ReadLine() ) { Console::WriteLine( s ); } } finally { if ( sr ) delete (IDisposable^)sr; } try { String^ path2 = Path::GetTempFileName(); FileInfo^ fi2 = gcnew FileInfo( path2 ); //Ensure that the target does not exist. fi2->Delete(); //Copy the file. fi1->CopyTo( path2 ); Console::WriteLine( "{0} was copied to {1}.", path, path2 ); //Delete the newly created file. fi2->Delete(); Console::WriteLine( "{0} was successfully deleted.", path2 ); } catch ( Exception^ e ) { Console::WriteLine( "The process failed: {0}", e ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { String path = "c:\\temp\\MyTest.txt"; FileInfo fi1 = new FileInfo(path); if (!(fi1.get_Exists())) { //Create a file to write to. StreamWriter sw = fi1.CreateText(); try { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } finally { sw.Dispose(); } } //Open the file to read from. StreamReader sr = fi1.OpenText(); try { String s = ""; while ((s = sr.ReadLine())!= null) { Console.WriteLine(s); } } finally { sr.Dispose(); } try { String path2 = path + "temp"; FileInfo fi2 = new FileInfo(path2); //Ensure that the target does not exist. fi2.Delete(); //Copy the file. fi1.CopyTo(path2); Console.WriteLine("{0} was copied to {1}.", path, path2); //Delete the newly created file. fi2.Delete(); Console.WriteLine("{0} was successfully deleted.", path2); } catch (System.Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } //main } //Test
System.MarshalByRefObject
System.IO.FileSystemInfo
System.IO.FileInfo
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- FileInfo クラスのページへのリンク