OpenFile Dialog in c#

In this article, we will learn how to use open file dialog box in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a Openfile dialog then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "OpenFileDialog".

openfile dialog
  1. First of all, we'll know about the "Openfile" dialog.

    The open file dialog is mainly used to open any file to memory in c#. We'll discuss properties and function and events of open file dialog write down.

    The following code snippet creates a OpenFileDialog control object under btnsave "OnClick" methods.

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    

    ShowDialog() method displays the OpenFileDialog

    openFileDialog1.ShowDialog();
    

    OpenFileDialog Initial directory are used to gets or sets the initial directory displayed by the file dialog box.

    openFileDialog1.InitialDirectory = @"F:\";
    

    OpenFileDialog RestoreDirectory are used to get or sets a value indicating whether the dialog box restores the current directory before closing.

    openFileDialog1.RestoreDirectory = true;
    

    OpenFileDialog Filter are used to get or sets the current file name filter string. which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.

    openFileDialog1.Filter = "txt files (*.txt)|*.txt";
    

    OpenFileDialog FilterIndex are used to get or sets the index of the filter currently selected in the dialog box.

    openFileDialog1.FilterIndex = 1;
    

    OpenFileDialog Title are used to get or sets the file dialog box title.

    openFileDialog1.Title = "Open Your File";
    

    OpenFileDialog DefaultExt are used to get or sets the default file extension.

    openFileDialog1.DefaultExt = "*.txt";
    

    OpenFileDialog CheckFileExist are used to get or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.

    if (openFileDialog1.CheckFileExists == true)
    

    OpenFileDialog CheckPathExist are used to get or sets a value indicating whether the dialog box displays a warning if the user specifies a file path that does not exist.

    if (openFileDialog1.CheckPathExists == true)
    

    OpenFileDialog FileName are used to get or sets a string containing the file name selected in the file dialog box.

    openFileDialog1.FileName = "DefaultFile";
    

    OpenFileDialog OpenFile() opens the file selected by the user, with read only permission.

    openFileDialog1.OpenFile();
    

    OpenFileDialog Reset() are used to reset all dialog box option to their default values.

    openFileDialog1.Reset();
    


Write a comment


Name Email Message