C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Winforms Controls »

Open File Dialog Box


Posted Date: 09 Apr 2009    Resource Type: Code Snippets    Category: Winforms Controls
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 10



OpenFile Dialog Box to select a file. For a wrong input (filename) it will show a error message.


private void btnOpenTextFile_Click(object sender, System.EventArgs e)
{
StreamReader ts = null;
try
{
odlgTextFile.CheckFileExists = true;

// Check to ensure that the selected
// path exists. Dialog box displays
// a warning otherwise.

odlgTextFile.CheckPathExists = true;

odlgTextFile.DefaultExt = "txt";

// return the file referenced by a link? if
// false, simply returns the selected link
// file. if true, returns the file linked to
// the LNK file.

odlgTextFile.DereferenceLinks = true;

// Just in VB6, use a set of pairs
// of filters, separated with "|". Each
// pair consists of a description|file spec.
// Use a "|" between pairs. No need to put a
// trailing "|". You can set the FilterIndex
// property well, to select the default
// filter. Amazingly, the first filter is
// numbered 1 (! 0). The default is 1.

odlgTextFile.Filter = "Text files (*.txt)|*.txt|" + "All files|*.*";

odlgTextFile.Multiselect = false;

// Restore the original directory when done selecting
// a file? if false, the current directory changes
// to the directory in which you selected the file.
// Set this to true to put the current folder back
// where it was when you started.
// The default is false.

odlgTextFile.RestoreDirectory = true;

// Show the Help button and Read-Only checkbox?

odlgTextFile.ShowHelp = true;

odlgTextFile.ShowReadOnly = false;

// Start out with the read-only check box checked?
// This only make sense if ShowReadOnly is true.
// .ReadOnlyChecked = false

odlgTextFile.Title = "Select a file to open";

// Only accept valid Win32 file names?

odlgTextFile.ValidateNames = true;

if (odlgTextFile.ShowDialog() == DialogResult.OK)
{
FileName = odlgTextFile.FileName;
ts = new StreamReader(odlgTextFile.OpenFile());
txtFileContents.Text = ts.ReadToEnd();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, this.Text);
}
finally
{
if(ts != null)
{
ts.Close();
}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Winforms  .  Open File Dailog  .  Controls  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Number to Words
Previous Resource: Open a new window by button click in parent form in winforms application
Return to Discussion Resource Index
Post New Resource
Category: Winforms Controls


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use