FileInfo.Open メソッド
さまざまな読み書き権と共有権でファイルを開きます。
オーバーロードの一覧
指定したモードでファイルを開きます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Open(FileMode) As FileStream
読み取り可、書き込み可、または読み書き込み可などのアクセス権を指定し、指定したモードでファイルを開きます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Open(FileMode, FileAccess) As FileStream
[JScript] public function Open(FileMode, FileAccess) : FileStream;
読み取り可、書き込み可、読み書き可などのアクセス権を指定し、指定したモードと共有オプションでファイルを開きます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Open(FileMode, FileAccess, FileShare) As FileStream
[C#] public FileStream Open(FileMode, FileAccess, FileShare);
[C++] public: FileStream* Open(FileMode, FileAccess, FileShare);
[JScript] public function Open(FileMode, FileAccess, FileShare) : FileStream;
使用例
他のユーザーまたはプロセスからのアクセスを禁止して、ファイルと読み取りおよび書き込み用に開く例を次に示します。
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();