OpenFileDialog In WinForms Windows Forms Application C# VB.NET

OpenFileDialog In WinForms Windows Forms Application Using C# And VB.NET

In this example i m creating a Open file dialog box to browse and select a file to open in .NET windows applications using C# and winforms




Start a new Windows Application project and drag a button, double click on the button to generate it's click event

Now create a new openFileDialog

OpenFileDialog fDialog = new OpenFileDialog();

To set the title of window
fDialog.Title = "Open Image Files";

To apply filter, which only allows the files with the name or extension specified to be selected. in this example i m only using jpeg and GIF files
fDialog.Filter = "JPEG Files|*.jpeg|GIF Files|*.gif";

To set the Initial Directory property ,means which directory to show when the open file dialog windows opens
fDialog.InitialDirectory = @"C:\";

if the user has clicked the OK button after choosing a file,To display a MessageBox with the path of the file:
if(fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(fDialog.FileName.ToString());
}


If you want to select multiple files ,set the Multiselect property to true and to return the name of the files use fDialog.FileNames instead of fDialog.FileName. This will return a string array with the name of the files.
Other properties which we can use are :
If a user types the name of a file but doesn't specify the extension you can set the AddExtension property to true:
fDialog.AddExtension = true;

if a user types the name of a file or path that does not exist we can give him an warning:
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;


If you like this post than join us or share

10 comments:

Anonymous said...

Thank you for nice post...you exaplained in easy way


Anonymous said...

Thanks!1


Anonymous said...

if any body has code which stops redirecting to login page after user clicked on logout button
because after logout by user any one by clicking back button in IE it will take you to last visited page i wanted to avoid this through c# code
please suggest me some solution


Anonymous said...

In windows form application in .net I want to open a file from hard disk. in order to do that i kept a browse button but i'am not getting how to write the code for that button can any one help me for that
thanks and regards from
C.Janardhan Reddy


simonessalamone said...

asp.net file picker


FileUltimate is an ASP.NET file manager control which you can add directly to your existing ASP.NET (.aspx) pages. The control renders a user interface similar to "Windows Explorer" within the page and this user interface allows you to view the contents of the predefined root folders and allow complete file management actions within those folders. File management actions can be limited by predefined permissions and quota limits on each folder separately.


Anonymous said...

hello....
i have got a project of " Online University System".in which i want that the teachers and the atusdents have to register to the website and then the teacher post the lecture to the university website and the students can take the lecture. and i have also mentioned about the discussion board.
m using C# in .Net framework.
i want you to suggest me , how to proceed..............thanx


Asha said...

Hello Everyone,

Anyone knows how to Open the OpenDialogBox to attach a file in ASP.Net Web Application.i have tried with Windows Application it works properly.I was trying to do it in Web application i am not getting it. Can anyone Explain it how can we do it in Web Application.


Asha said...

I know that we cant use Openfiledialog in Web application but is there any alternative way to do that by using java script or something like that.


Unknown said...

@Asha: In web applications we use FileUpload Control to upload files on server it automatically opens browse file dialog box


Anonymous said...

Thanx..........................


Find More Articles