I try to open temporary HTML file with default browser and delete the file then:
var tempFileName = Path.ChangeExtension(Path.GetTempFileName(), "html");
// I save document to temp file here...
Process process = null;
try
{
process = Process.Start(tempFileName);
}
catch (Win32Exception)
{
}
catch (ObjectDisposedException)
{
}
catch (FileNotFoundException)
{
}
var worker = new BackgroundWorker();
worker.DoWork += (s, we) => {
if (process != null)
{
process.WaitForExit();
try
{
File.Delete(tempFileName);
}
catch (IOException)
{
}
}
};
worker.RunWorkerAsync();
Unfortunately, Process.Start
returns null if a process is not started, but a running one is used (new tab is opened in Google Chrome). So I can't wait for that process to exit.
So, a general question is: how to do the task? How to show a temporary HTML file to a user and delete it after viewing?
You can also read the html content into a Memory Stream or into a string variable using WebClient, and after close the Stream or WebClient, file will be released, ready to be deleted, as you no longer need it.
Them you have the html content in memory, just send it to browser.
Here some example if you need:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/060ea8e0-cc63-44a3-b0dc-b531c29b8a0f/read-html-content-using-cnet?forum=csharpgeneral
Hope it helps.