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

File クラス

ファイル作成コピー削除移動オープンのための静的メソッド提供し、FileStream オブジェクトの作成支援します

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

<ComVisibleAttribute(True)> _
Public NotInheritable Class
 File
[ComVisibleAttribute(true)] 
public static class File
[ComVisibleAttribute(true)] 
public ref class File abstract sealed
/** @attribute ComVisibleAttribute(true) */ 
public final class File
ComVisibleAttribute(true) 
public final class File
解説解説

File クラスは、ファイルコピー移動、名前変更作成オープン削除内容追加などの一般的な操作使用しますFile クラスは、ファイル作成ファイルへのアクセス、およびファイルへの書き込み関連した DateTime 情報、およびファイル属性取得設定にも使用できます

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

Fileメソッドはすべて静的であるため、1 つ操作実行するだけであればFileメソッド使用する方が、対応する FileInfoインスタンス メソッド使用するよりも効率的な場合ありますFile メソッドはすべて、操作するファイルパスを必要とします

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

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

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

メモメモ

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

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Dim path As String
 = "c:\temp\MyTest.txt"
        If File.Exists(path) = False Then
            ' Create a file to write to.
            Dim sw As StreamWriter = File.CreateText(path)
            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 = File.OpenText(path)
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
            Dim path2 As String
 = path + "temp"

            ' Ensure that the target does not exist.
            File.Delete(path2)

            ' Copy the file.
            File.Copy(path, path2)
            Console.WriteLine("{0} was copied to {1}.",
 path, path2)

            ' Delete the newly created file.
            File.Delete(path2)
            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 = @"c:\temp\MyTest.txt";
        if (!File.Exists(path)) 
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }    
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
 
            {
                Console.WriteLine(s);
            }
        }

        try 
        {
            string path2 = path + "temp";
            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            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 = "c:\\temp\\MyTest.txt";
   if (  !File::Exists( path ) )
   {
      
      // Create a file to write to.
      StreamWriter^ sw = File::CreateText( path );
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
                  delete (IDisposable^)(sw);
      }
   }

   // Open the file to read from.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
            delete (IDisposable^)(sr);
   }

   try
   {
      String^ path2 = String::Concat( path, "temp" );
      
      // Ensure that the target does not exist.
      File::Delete( path2 );
      
      // Copy the file.
      File::Copy( path, path2 );
      Console::WriteLine( "{0} was copied to {1}.", path, path2 );
      
      // Delete the newly created file.
      File::Delete( path2 );
      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";

        if (!(File.Exists(path))) {
            // Create a file to write to.
            StreamWriter sw = File.CreateText(path);

            try {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
            finally {
                sw.Dispose();
            }
        }

        // Open the file to read from.
        StreamReader sr = File.OpenText(path);

        try {
            String s = "";

            while ((s = sr.ReadLine()) != null)
 {
                Console.WriteLine(s);
            }
        }
        finally {
            sr.Dispose();
        }
        try {
            String path2 = path + "temp";

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test
継承階層継承階層
System.Object
  System.IO.File
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

「File クラス」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS