Fileinfo 関数
導入
このモジュールの関数は、ファイル内の特定の位置から magic バイトシーケンスを見つけることで、 ファイルの content type とエンコーディングを推測します。 これは完全な手法ではありませんが、経験上かなりうまく動作しています。要件
この拡張モジュールをビルドするには magic_open ライブラリが必要です。インストール手順
簡単なインストールメモ。コンソールで、ただ単に$ pear install fileinfoとタイプします。
実行時設定
設定ディレクティブは定義されていません。リソース型
Fileinfo 拡張モジュールでは、一種類のリソースが使用されています。 それは、finfo_open() が返す magic データベース記述子です。定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。- FILEINFO_NONE (integer)
- 特別な処理を行いません。
- FILEINFO_SYMLINK (integer)
- シンボリックリンクのリンク先をたどります。
- FILEINFO_MIME (integer)
- テキスト表現ではなく、mime 文字列を返します。
- FILEINFO_COMPRESS (integer)
- 圧縮されたファイルを伸張します。
- FILEINFO_DEVICES (integer)
- ブロックデバイスあるいはキャラクタデバイスの内容を探します。
- FILEINFO_CONTINUE (integer)
- 最初に見つかったものだけでなく、一致するものをすべて返します。
- FILEINFO_PRESERVE_ATIME (integer)
- 可能な限り、元の最終アクセス時刻を保持します。
- FILEINFO_RAW (integer)
- 表示できない文字を \ooo 形式の 8 進表現に変換しません。
目次
- finfo_buffer — 文字列バッファの情報を返す
- finfo_close — fileinfo リソースを閉じる
- finfo_file — ファイルについての情報を返す
- finfo_open — 新しい fileinfo リソースを作成する
- finfo_set_flags — libmagic のオプションを設定する
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 コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)



完全修飾ファイル名または相対ファイル名のいずれかを指定できますが、セキュリティ チェックでは完全限定名が採用されます。
その他の一般的な 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 |

このコンストラクタを使用して 2 つのファイルを作成し、それらに対して書き込み、読み取り、コピー、および削除を実行する例を次に示します。
Imports System Imports System.IO Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Dim fi1 As FileInfo = New FileInfo(path) If fi1.Exists = False Then 'Create a file to write to. Dim sw As StreamWriter = fi1.CreateText() sw.WriteLine("Hello") sw.WriteLine("And") sw.WriteLine("Welcome") sw.Flush() sw.Close() End If 'Open the file to read from. Dim sr As StreamReader = fi1.OpenText() Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop Try Dim path2 As String = path + "temp" Dim fi2 As FileInfo = 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 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"; 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 + "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 (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"; 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 = String::Concat( path, "temp" ); 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
既存ファイルを開くか、またはファイルを作成し、ファイルにテキストを追加し、結果を表示する例を次に示します。
Imports System Imports System.IO Public Class FileInfoMainTest Public Shared Sub Main() ' Open an existing file, or create a new one. Dim fi As New FileInfo("temp.txt") ' Create a writer, ready to add entries to the file. Dim sw As StreamWriter = fi.AppendText() sw.WriteLine("This is a new entry to add to the file") sw.WriteLine("This is yet another line to add...") sw.Flush() sw.Close() Dim sr As New StreamReader(fi.OpenRead()) ' Get the information out of the file and display it. While sr.Peek() <> -1 Console.WriteLine(sr.ReadLine()) End While End Sub 'Main End Class 'FileInfoMainTest
using System; using System.IO; public class FileInfoMainTest { public static void Main() { // Open an existing file, or create a new one. FileInfo fi = new FileInfo("temp.txt"); // Create a writer, ready to add entries to the file. StreamWriter sw = fi.AppendText(); sw.WriteLine("This is a new entry to add to the file"); sw.WriteLine("This is yet another line to add..."); sw.Flush(); sw.Close(); // Get the information out of the file and display it. StreamReader sr = new StreamReader( fi.OpenRead() ); while (sr.Peek() != -1) Console.WriteLine( sr.ReadLine() ); } }
using namespace System; using namespace System::IO; int main() { // Open an existing file, or create a new one. FileInfo^ fi = gcnew FileInfo( "temp.txt" ); // Create a writer, ready to add entries to the file. StreamWriter^ sw = fi->AppendText(); sw->WriteLine( "This is a new entry to add to the file" ); sw->WriteLine( "This is yet another line to add..." ); sw->Flush(); sw->Close(); // Get the information out of the file and display it. StreamReader^ sr = gcnew StreamReader( fi->OpenRead() ); while ( sr->Peek() != -1 ) Console::WriteLine( sr->ReadLine() ); }
import System; import System.IO; public class FileInfoMainTest { public static function Main() : void { // Open an existing file, or create a new one. var fi : FileInfo = new FileInfo("temp.txt"); // Create a writer, ready to add entries to the file. var sw : StreamWriter = fi.AppendText(); sw.WriteLine("This is a new entry to add to the file"); sw.WriteLine("This is yet another line to add..."); sw.Flush(); sw.Close(); // Get the information out of the file and display it. var sr : StreamReader = new StreamReader( fi.OpenRead() ); while (sr.Peek() != -1) Console.WriteLine( sr.ReadLine() ); } } FileInfoMainTest.Main();


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 プロパティ

名前 | 説明 | |
---|---|---|
![]() | Attributes | 現在の FileSystemInfo の FileAttributes を取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | CreationTime | 現在の FileSystemInfo オブジェクトの作成日時を取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | CreationTimeUtc | 現在の FileSystemInfo オブジェクトの作成日時を世界協定時刻 (UTC) で取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | Exists | オーバーライドされます。 ファイルが存在するかどうかを示す値を取得します。 |
![]() | Extension | ファイルの拡張子部分を表す文字列を取得します。 ( FileSystemInfo から継承されます。) |
![]() | FullName | ディレクトリまたはファイルの絶対パスを取得します。 ( FileSystemInfo から継承されます。) |
![]() | LastAccessTime | 現在のファイルまたはディレクトリに最後にアクセスした時刻を取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | LastAccessTimeUtc | 現在のファイルまたはディレクトリに最後にアクセスした時刻を世界協定時刻 (UTC) で取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | LastWriteTime | 現在のファイルまたはディレクトリに最後に書き込みが行われた時刻を取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | LastWriteTimeUtc | 現在のファイルまたはディレクトリに最後に書き込みが行われた時刻を世界協定時刻 (UTC) で取得または設定します。 ( FileSystemInfo から継承されます。) |
![]() | Name | オーバーライドされます。 ファイルの名前を取得します。 |

FileInfo メソッド


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


名前 | 説明 | |
---|---|---|
![]() | Attributes | 現在の FileSystemInfo の FileAttributes を取得または設定します。(FileSystemInfo から継承されます。) |
![]() | CreationTime | 現在の FileSystemInfo オブジェクトの作成日時を取得または設定します。(FileSystemInfo から継承されます。) |
![]() | CreationTimeUtc | 現在の FileSystemInfo オブジェクトの作成日時を世界協定時刻 (UTC) で取得または設定します。(FileSystemInfo から継承されます。) |
![]() | Exists | オーバーライドされます。 ファイルが存在するかどうかを示す値を取得します。 |
![]() | Extension | ファイルの拡張子部分を表す文字列を取得します。(FileSystemInfo から継承されます。) |
![]() | FullName | ディレクトリまたはファイルの絶対パスを取得します。(FileSystemInfo から継承されます。) |
![]() | LastAccessTime | 現在のファイルまたはディレクトリに最後にアクセスした時刻を取得または設定します。(FileSystemInfo から継承されます。) |
![]() | LastAccessTimeUtc | 現在のファイルまたはディレクトリに最後にアクセスした時刻を世界協定時刻 (UTC) で取得または設定します。(FileSystemInfo から継承されます。) |
![]() | LastWriteTime | 現在のファイルまたはディレクトリに最後に書き込みが行われた時刻を取得または設定します。(FileSystemInfo から継承されます。) |
![]() | LastWriteTimeUtc | 現在のファイルまたはディレクトリに最後に書き込みが行われた時刻を世界協定時刻 (UTC) で取得または設定します。(FileSystemInfo から継承されます。) |
![]() | Name | オーバーライドされます。 ファイルの名前を取得します。 |


- FileInfoのページへのリンク