Note: I am assuming you have a form created in C#.

1) Create a Rich Text Box on the form. Right click on the box and open Properties. Under Design , change the name to: path (or whatever you want to call it)
2) Add a button to the form and name it “Browse”
3) Now double click on the button and code will open.
4) Inside the button_Click event, you want to post this code:

OpenFileDialog browseFile = new OpenFileDialog();
browseFile.Filter = "XML Files (*.xml)|*.xml";
browseFile.Title = "Browse XML file";
if (browseFile.ShowDialog() == DialogResult.Cancel)
return;
try
{
path.Text = browseFile.FileName;
}
catch (Exception)
{
MessageBox.Show("Error opening file", "File Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

This code is for XML File but you can change the value in the Filter for any other type of file.

This entry was posted in C#, QC, Useful Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s