FileInfo.Open メソッド (FileMode, FileAccess, FileShare)

読み取り可、書き込み可、読み書き可などのアクセス権を指定し、指定したモードと共有オプションでファイルを開きます。

Overloads Public Function Open( _
   ByVal mode As FileMode, _   ByVal access As FileAccess, _   ByVal share As FileShare _) As FileStream
[C#]
public FileStream Open(FileModemode,FileAccessaccess,FileShareshare);
[C++]
public: FileStream* Open(FileModemode,FileAccessaccess,FileShareshare);
[JScript]
public function Open(
   mode : FileMode,access : FileAccess,share : FileShare) : FileStream;

パラメータ

  • mode
    ファイルを開くときのモード (Open または Append など) を指定する FileMode 定数。
  • access
    ファイルを ReadWrite 、または ReadWrite のいずれのアクセス権で開くかを指定する FileAccess 定数。
  • share
    ファイルに対して他の FileStream オブジェクトが持つアクセスの種類を指定する FileShare 定数。

戻り値

指定したモード、アクセス権、および共有オプションで開く FileStream オブジェクト。

例外

例外の種類 条件
SecurityException 呼び出し元に、必要なアクセス許可がありません。
ArgumentException path が空か、空白文字だけを含んでいます。
FileNotFoundException ファイルが見つかりません。
ArgumentNullException 1 つ以上の引数が null 参照 (Visual Basic では Nothing) です。
UnauthorizedAccessException path が読み取り専用か、またはディレクトリです。
DirectoryNotFoundException 割り当てられていないドライブであるなど、指定されたパスが無効です。
IOException ファイルが既に開いています。

解説

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルの名前を変更、またはファイルを移動する。 File.Move

FileInfo.MoveTo

ファイルをコピーする。 File.Copy

FileInfo.CopyTo

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み
ディレクトリを作成する。 CreateDirectory

Directory

使用例

他のユーザーまたはプロセスからのアクセスを禁止して、ファイルと読み取りおよび書き込み用に開く例を次に示します。

 
Imports System
Imports System.IO

Public Class OpenTest

    Public Shared Sub Main()
        ' Open an existing file, or create a new one.
        Dim fi As New FileInfo("temp.txt")

        ' Open the file just specified such that no one else can use it.
        Dim fs As FileStream = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)

        ' Create another reference to the same file.
        Dim nextfi As New FileInfo("temp.txt")

        Try
            ' Try opening the same file, which was locked by the previous process.
            nextfi.Open(FileMode.OpenOrCreate, FileAccess.Read)
            Console.WriteLine("The file was not locked, and was opened by a second process.")
        Catch i as IOException
            Console.WriteLine(i.ToString())
        Catch e As Exception
            Console.WriteLine(e.ToString())
        End Try

        ' Close the file so it can be deleted.
        fs.Close()
    End Sub 'Main
End Class 'OpenTest

[C#] 
using System;
using System.IO;

public class OpenTest 
{
    public static void Main() 
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );

        // Create another reference to the same file.
        FileInfo nextfi = new FileInfo("temp.txt");        

        try 
        {
            // Try opening the same file, which was locked by the previous process.
            nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );

            Console.WriteLine("The file was not locked, and was opened by a second process.");
        } 
        catch (IOException) 
        {
            Console.WriteLine("The file could not be opened because it was locked by another process.");
        } 
        catch (Exception e) 
        {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Open an existing file, or create a new one.
    FileInfo* fi = new FileInfo(S"temp.txt");

    // Open the file just specified such that no one else can use it.
    FileStream* fs = fi->Open(FileMode::OpenOrCreate, FileAccess::ReadWrite, FileShare::None);

    // Create another reference to the same file.
    FileInfo* nextfi = new FileInfo(S"temp.txt");

    try {
        // Try opening the same file, which was locked by the previous process.
        nextfi->Open(FileMode::OpenOrCreate, FileAccess::Read);
        Console::WriteLine(S"The file was not locked, and was opened by a second process.");
    } catch (IOException*) {
        Console::WriteLine(S"The file could not be opened because it was locked by another process.");
    } catch (Exception* e) {
        Console::WriteLine(e);
    }

    // Close the file so it can be deleted.
    fs->Close();
}

[JScript] 
import System;
import System.IO;

public class OpenTest {
    public static function Main() : void {

        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        var fs : FileStream = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );

        // Create another reference to the same file.
        var nextfi : FileInfo = new FileInfo("temp.txt");        

        try {
            // Try opening the same file, which was locked by the previous process.
            nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );

            Console.WriteLine("The file was not locked, and was opened by a second process.");
        } catch (e : IOException) {
            Console.WriteLine("The file could not be opened because it was locked by another process.");
        } catch (e : Exception) {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    }
}
OpenTest.Main();

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

FileInfo クラス | FileInfo メンバ | System.IO 名前空間 | FileInfo.Open オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み