2

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?

Artem Kachanovskyi
  • 1,839
  • 2
  • 18
  • 27

3 Answers3

1

If you use ProcessStartInfo and set UseShellExecute then you can start the user's default browser by "running" the HTML directly like you're trying to do now. I haven't tried it, but it should give you a Process back to determine when the user has closed the browser.

I would still prepare for a bunch of edge cases you have no control over. Such as if they leave the browser open but close the app that's watching the browser. At that point do you let the browser stay alive? Do you kill it? When do you delete the HTML file? It might be better to use the Web Browser control. Then you don't even have to worry about other processes or browser compatibility. You can even stream the HTML contents to the control and there is no file to delete later.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • Using `ShellExecute` property does not help: when I set it to `false` the file is not open at all. As for `WebBrowser`, I don't really like this solution: it's dependent on IE and as far as I know in `WebBrowser` it's not so easy to use all the abilities that browser brings: toolbars, tabs and so on. But you are right, user might close the program and keep the browser. Then there is nothing to do actually. One more possible solution would be to delete the file on application exit... – Artem Kachanovskyi Jun 16 '15 at 14:32
  • 1
    It shouldn't be false. You do need to use the shell so it has to be true. But it sounds like you'll be back in the boat you're in now (Process.Start returning null) – Corey Ogburn Jun 16 '15 at 14:34
  • Yes, it's so. Returned `Process` is null. – Artem Kachanovskyi Jun 16 '15 at 14:35
0

You can force a new browser instance, by first figuring out the default browser, and executing it manually:

public Process launchBrowser(string url)
{
    string browserName = "iexplore.exe";
    using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
    {
        if (userChoiceKey != null)
        {
            object progIdValue = userChoiceKey.GetValue("Progid");
            if (progIdValue != null)
            {
                if(progIdValue.ToString().ToLower().Contains("chrome"))
                    browserName = "chrome.exe";
                else if(progIdValue.ToString().ToLower().Contains("firefox"))
                    browserName = "firefox.exe";
                else if (progIdValue.ToString().ToLower().Contains("safari"))
                    browserName = "safari.exe";
                else if (progIdValue.ToString().ToLower().Contains("opera"))
                    browserName = "opera.exe";
            }
        }
    }

    return Process.Start(new ProcessStartInfo(browserName, url));
}

Then you can get a handle to the process:

var process = launchBrowser("www.google.com");

process.WaitForExit();

try
{
    //Do whatever
}
catch (IOException)
{
}
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
-1

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.

Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43