1

Opening a Webdocument in the default browser is a qeustion that have been answered a minnion times. But I have a twist to the question:

How can I in C# open or Refresh a html document in the default browser.

If I try some thing like this:

private Process ShowFile(string htmlFilename)
{
var myProcess = new Process();

    try
    {
        // true is the default, but it is important not to set it to false
        myProcess.StartInfo.UseShellExecute = true;
        myProcess.StartInfo.FileName = htmlFilename;
        myProcess.Start();
    }
    catch (Exception e)
    {

    }

    return myProcess;
}

A new window is opened each time.

* EDIT *

I have tried this but the Browser doesn't refresh

public delegate bool EnumThreadWindowsCallback(int hWnd, int lParam);

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

    [DllImport("user32.dll")]
    static extern int GetWindowText(int hWnd, StringBuilder text, int count);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static public extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);


    private bool FindWindowByRx(int hWnd, int lParam)
    {
        Regex pattern = new Regex("Seneste nyt", RegexOptions.IgnoreCase);
        StringBuilder windowTitle = new StringBuilder(256);
        GetWindowText(hWnd, windowTitle, 255);

        if (pattern.IsMatch(windowTitle.ToString()))
        {
            SetForegroundWindow(new IntPtr(hWnd));
            return false; // abort search
        }
        else
        {
            return true; // keep on searching
        }
    }

    private bool ActivateWindow()
    {
        return EnumWindows(new EnumThreadWindowsCallback(FindWindowByRx), new IntPtr());

        uint KEYEVENTF_KEYUP = 2;
        byte VK_CONTROL = 0x11;
        byte VK_F5= 0x74;

        keybd_event(VK_CONTROL, 0, 0, 0);
        keybd_event(VK_F5, 0, 0, 0);             
        keybd_event(VK_F5, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    }

It only gets focus.

Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • 1
    You have a `return` statement just at the beginning of your `ActivateWindow()` function. – mihai Oct 25 '14 at 16:00
  • When removing that it works :D – Jens Borrisholt Oct 25 '14 at 16:04
  • 1
    Great. By the way, you can [treat warning as errors](http://stackoverflow.com/questions/2520853/warning-as-error-how-to-rid-these). That `return` statement would have caused Visual Studio to give an `Unreachable code detected` warning. If the `treat warnings as errors` was set to `All`, your code wouldn't have compiled. Thus, less time on SO, more time coding :). – mihai Oct 25 '14 at 16:07
  • I'll will do that right away. I just haven thought about it. Wich is strange, becaus I've coded Delphi for many years and my Delphi is allways setup to treat both hints and watnings as errors. – Jens Borrisholt Oct 25 '14 at 16:16

1 Answers1

2

First, the browser tab containing the page you want to refresh must be the selected tab. According to this SO question you can't activate the tab containing a certain url. You can find all the urls of the opened tabs but you can't select the tab you want.

So, assuming the tab is already selected, you can just find and activate the browser's window and send it a combination of keys similar to Get the selected Text from other Process. Only instead of ctrl+c you can send a ctr+l (this puts the cursor on the address field) and enter (this will refresh the page) for Firefox, for example.

Alternatively, also for Firefox at least, you can send a ctrl+w, which closes the current selected tab, and then open a new page as your code already does, which in your case will be exactly the same page you've just closed.

Or, you can open your page in a new browser instance (not a new tab). When you want to refresh it, you can find that browser window by name and close it. Then reopen a new browser window. This would be a solution in case the tab you're interested might not currenty be the selected tab in your browser

Community
  • 1
  • 1
mihai
  • 4,592
  • 3
  • 29
  • 42