File クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
解説
File クラスは、ファイルのコピー、移動、名前変更、作成、オープン、削除、内容の追加などの一般的な操作に使用します。File クラスは、ファイルの作成、ファイルへのアクセス、およびファイルへの書き込みに関連した DateTime 情報、およびファイル属性の取得と設定にも使用できます。
File のメソッドの多くは、ファイルを作成またはオープンしたときにそれぞれに異なる I/O 型を返します。これらの型は、以降のファイル操作に使用できます。詳細については、OpenText、CreateText、Create などの具体的な File メンバのトピックを参照してください。
File のメソッドはすべて静的であるため、1 つの操作を実行するだけであれば、File のメソッドを使用する方が、対応する FileInfo のインスタンス メソッドを使用するよりも効率的な場合があります。File メソッドはすべて、操作するファイルのパスを必要とします。
File クラスの静的メソッドは、すべてのメソッドでセキュリティ チェックを実行します。オブジェクトを何回か再利用する場合は、このようなセキュリティ チェックが必ずしも必要ではなくなるため、これらの静的メソッドの代わりに FileInfo の対応するインスタンス メソッドを使用することを検討してください。
既定では、すべてのユーザーに、新しいファイルに対する完全な読み書きアクセス権が与えられます。
さまざまな File メソッドの動作をカスタマイズするために使用する列挙体を次の表に示します。
列挙体 | |
---|---|
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 |
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.IO.File
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
File メソッド
パブリック メソッド
File メンバ
ファイルの作成、コピー、削除、移動、オープンのための静的メソッドを提供し、FileStream オブジェクトの作成を支援します。
パブリック メソッド
固有名詞の分類
Weblioに収録されているすべての辞書からfileを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からfileを検索
「file」に関係したコラム
FXのチャート分析ソフトMT4で10分足や2時間足などを表示するには
FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)では、次の時間足の表示ができます。ティック1分足5分足15分足30分足1時間足4時間足日足週足MT4では、10分足や1...
FXのチャート分析ソフトMT4のインディケーターで矢印を表示するには
FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)にはさまざまなインディケーターが用意されています。インディケーターは、売買のエントリーポイントを見つける手段の1つとし...
-
FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)のインディケーターの作り方について解説します。インディケーターの作り方の大まかな流れは次の通りです。MetaEdito...
-
FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)のEA(Expert Advisor)の作り方を紹介します。MT4のメニューから「ツール」、「MetaQuotes L...
- fileのページへのリンク