OpenFileDialog.OpenFile メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As OpenFileDialog Dim returnValue As Stream returnValue = instance.OpenFile
ユーザーが選択した読み取り専用ファイルを指定する Stream。


ダイアログ ボックスからファイルをすばやく開くには、OpenFile メソッドを使用します。この場合、ファイルはセキュリティを維持するために読み取り専用モードで開かれます。ファイルを読み取り/書き込みモードで開くには、FileStream などの別のメソッドを呼び出す必要があります。

OpenFile メソッドを使用する方法を次のコード例に示します。この例では、ShowReadOnly プロパティが true に設定された OpenFileDialog を表示します。オプションをクリックしてファイルを読み取り専用モードで開くと、OpenFile メソッドを使用してファイルが開かれます。それ以外の場合は、FileStream クラスを使用して読み取り/書き込みモードでファイルが開かれます。
Private Function OpenFile() As FileStream ' Displays an OpenFileDialog and shows the read/only files. Dim DlgOpenFile As New OpenFileDialog() DlgOpenFile.ShowReadOnly = True Dim Fs As FileStream If DlgOpenFile.ShowDialog() = DialogResult.OK Then ' If ReadOnlyChecked is true, uses the OpenFile method to ' open the file with read/only access. If DlgOpenFile.ReadOnlyChecked = True Then Return DlgOpenFile.OpenFile() ' Otherwise, opens the file with read/write access. Else Dim Path As String = DlgOpenFile.FileName Return New FileStream(Path, System.IO.FileMode.Open, _ System.IO.FileAccess.ReadWrite) End If End If End Function
private FileStream OpenFile() { // Displays an OpenFileDialog and shows the read/only files. OpenFileDialog dlgOpenFile = new OpenFileDialog(); dlgOpenFile.ShowReadOnly = true; if(dlgOpenFile.ShowDialog() == DialogResult.OK) { // If ReadOnlyChecked is true, uses the OpenFile method to // open the file with read/only access. if(dlgOpenFile.ReadOnlyChecked == true) { return (FileStream)dlgOpenFile.OpenFile(); } // Otherwise, opens the file with read/write access. else { string path = dlgOpenFile.FileName; return new FileStream(path, System.IO.FileMode.Open , System.IO.FileAccess.ReadWrite); } } return null; }
private: FileStream^ OpenFile() { // Displays an OpenFileDialog and shows the read/only files. OpenFileDialog^ dlgOpenFile = gcnew OpenFileDialog; dlgOpenFile->ShowReadOnly = true; if ( dlgOpenFile->ShowDialog() == ::DialogResult::OK ) { // If ReadOnlyChecked is true, uses the OpenFile method to // open the file with read/only access. if ( dlgOpenFile->ReadOnlyChecked == true ) { return dynamic_cast<FileStream^>(dlgOpenFile->OpenFile()); } // Otherwise, opens the file with read/write access. else { String^ path = dlgOpenFile->FileName; return gcnew FileStream( path,System::IO::FileMode::Open,System::IO::FileAccess::ReadWrite ); } } return nullptr; }
private FileStream OpenFile() { // Displays an OpenFileDialog and shows the read/only files. OpenFileDialog dlgOpenFile = new OpenFileDialog(); dlgOpenFile.set_ShowReadOnly(true); if (dlgOpenFile.ShowDialog().Equals(get_DialogResult().OK)) { // If ReadOnlyChecked is true, uses the OpenFile method to // open the file with read/only access. if (dlgOpenFile.get_ReadOnlyChecked() == true) { return (FileStream)dlgOpenFile.OpenFile(); } // Otherwise, opens the file with read/write access. else { String path = dlgOpenFile.get_FileName(); return new FileStream(path, System.IO.FileMode.Open , System.IO.FileAccess.ReadWrite); } } return null; } //OpenFile


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- OpenFileDialog.OpenFile メソッドのページへのリンク