OpenFileDialog.OpenFile Method

Opens the file selected by the user, with read-only permission. The file is specified by the FileName property.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public:
Stream^ OpenFile ()
public Stream OpenFile ()
public function OpenFile () : Stream

Return Value

A Stream that specifies the read-only file selected by the user.

Exception typeCondition

ArgumentNullException

The file name is a null reference (Nothing in Visual Basic).

The OpenFile method is used to provide a facility to quickly open a file from the dialog box. The file is opened in read-only mode for security purposes. To open a file in read/write mode, you must use another method, such as FileStream.

The following code example demonstrates how to use the OpenFile method. This example displays an OpenFileDialog with the ShowReadOnly property set to true. If the user clicks the option to open the file in read-only mode, the OpenFile method is used to open the file. Otherwise, the FileStream class is used to open the file in read/write mode.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show: