Open New Browser on Error

T

Tim Cowan

Hi
I have the following code to write an error file and it is supposed to open
the HTML file in a browser when an error occurs. It creates the file Ok but
doesn't open the new browser and doesn't give me any errors.

Tim

FileStream file = new FileStream(@"C:\errors\error.htm",
System.IO.FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(file);
sw.Write(strHtml.ToString());
System.Diagnostics.Process.Start(@"C:\errors\error.htm");
sw.Close();
file.Close();
 
J

Jim Hughes

Try closing the file before you start the browser.

You may also need to use sw.Flush();

sw.Close();
file.Close();
System.Diagnostics.Process.Start(@"C:\errors\error.htm");
 
T

Tim Cowan

I tried that Jim, both with and without sw.Flush(), but still no luck. Any
other ideas?
 
J

Jim Hughes

Does typing C:\errors\error.htm from a command prompt work?

The following code works fine on my system.

private void Form1_Load(object sender, System.EventArgs e)

{

String strHtml = "<html><body>Testing</body></html>";

FileStream file = new FileStream(@"C:\errors\error.htm",

System.IO.FileMode.OpenOrCreate);

StreamWriter sw = new StreamWriter(file);

sw.Write(strHtml.ToString());

System.Diagnostics.Process.Start(@"C:\errors\error.htm");

sw.Close();

file.Close();

}
 
T

Tim Cowan

I can open it from the command line but in the code it doesn't work. I am
running a C# web application in debug mode - would this make any difference?

Tim
 
J

Jim Hughes

Yes that would make a huge difference!

Web applications don't interact with the server desktop and therefore won't
launch the browser at the server.

If you are trying to use Process.Start on the client's browser, that won't
work either because of security.

The C# code is running inside of IIS at the server, not the client.

To open the error log in the browser:

Either use Response.Redirect to a server url that contains your error log

Or

Do a Resonse.Write of the file contents.

Or

use Javascript to do a window.open(errorurl)
 
T

Tim Cowan

Thanks Jim,

I am going to try that.

Tim

Jim Hughes said:
Yes that would make a huge difference!

Web applications don't interact with the server desktop and therefore
won't launch the browser at the server.

If you are trying to use Process.Start on the client's browser, that won't
work either because of security.

The C# code is running inside of IIS at the server, not the client.

To open the error log in the browser:

Either use Response.Redirect to a server url that contains your error log

Or

Do a Resonse.Write of the file contents.

Or

use Javascript to do a window.open(errorurl)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top