C# Programming in visual studio - how to open a local file in a web browser

James Hatfield

Reputable
Apr 29, 2015
9
0
4,510
Hi i have a webpage that is on my computer that i want to open in a webbrowser here is what i have tried


private void openWebPageToolStripMenuItem_Click(object sender, EventArgs e)
{
int size = -1;
openFileDialog2.InitialDirectory = "C://";
openFileDialog2.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*";
openFileDialog2.FilterIndex = 2;
openFileDialog2.RestoreDirectory = true;
openFileDialog2.Multiselect = false;
openFileDialog2.Title = "Open HTML File";
DialogResult result = openFileDialog2.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog2.FileName;
try
{
string htmlfile = File.ReadAllText(file);
size = htmlfile.Length;
filelocation = openFileDialog2.FileName;
Form2 frm2 = new Form2();
frm2.Show();
string a = openFileDialog2.FileName;
this.webBrowser1.Url = new System.Uri("file:///" + a, System.UriKind.Absolute);


}
catch (IOException)
{
}

I'm stuck and cant think of what to do and some help would be nice thanks.