Friday, December 12, 2008

OpenFileDialog in winforms windows forms C# .NET VB.NET windows application



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;




Add to diigo
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks Add to Mixx! Mixx it! add to del.icio.us saved by 0 users
Subscribe to Feeds

1 comments:

venkatesh said...

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


.NET Resources

Further Readings

Find More Articles


Followers

Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

Follow me on Twitter

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN