简体   繁体   中英

How to open HTML file with default browser and delete it after user viewed it?

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?

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.

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)
{
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM