FileInfo クラスとは? わかりやすく解説

FileInfo クラス

ファイル作成コピー削除移動、および開くためのインスタンス メソッド提供し、FileStream オブジェクト作成できるようにします。このクラス継承できません。

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

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 FileInfo
    Inherits FileSystemInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class FileInfo : FileSystemInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class FileInfo sealed : public
 FileSystemInfo
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FileInfo extends FileSystemInfo
SerializableAttribute 
ComVisibleAttribute(true) 
public final class FileInfo extends
 FileSystemInfo
解説解説

FileInfo クラスは、ファイルコピー移動、名前変更作成オープン削除内容追加などの一般的な操作使用します

FileInfoメソッド多くは、ファイル作成またはオープンしたときにそれぞれに異なI/O 型を返します。これらの型は、以降ファイル操作使用できます詳細については、Open、OpenRead、OpenText、CreateText、Create などの具体的な FileInfo メンバトピック参照してください

オブジェクト何回再利用する場合は、必ずしもセキュリティ チェックが必要ではなくなるため、File対応する静的メソッド代わりに FileInfoインスタンス メソッド使用することを検討してください

既定では、すべてのユーザーに、新しファイル対する完全な読み書きアクセス権与えられます。

さまざまな FileInfo メソッド動作カスタマイズするために使用する列挙体を次の表に示します

メモメモ

入力文字列としてパス受け入れメンバでは、そのパス正し書式である必要がありますそれ以外場合は、例外発生します。たとえば、パス絶対パスであっても空白始まっている場合、そのパスクラスメソッドではトリムされません。このためパス正し書式にならず、例外発生します同様に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 タスクの例を次の表に示します

Windows Mobile for Pocket PCWindows Mobile for SmartphoneWindows CE プラットフォームメモ : デバイスファイル システムはぞれぞれ動作異なるため、.NET Compact Framework ではファイル属性取得または設定サポートしていません。

使用例使用例

FileInfo クラス主要なメンバの例を次に示します

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.Object
   System.MarshalByRefObject
     System.IO.FileSystemInfo
      System.IO.FileInfo
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

「FileInfo クラス」の関連用語

FileInfo クラスのお隣キーワード
検索ランキング

   

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



FileInfo クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS