user3060735
user3060735

Reputation: 61

Refresh a web page programmatically

I would like to know if it is possible to refresh a web page from a click of a button from a winform app.

I have a form that when a press a button starts a process and shows a web page in a browser windows and I want to refresh it when I press another button instead of opening another windows.

Is it possible?

I've checked this question stackoverflow but thus it refresh the file I already call?

I launch the webpage like this:

string path = Directory.GetParent(Directory.GetCurrentDirectory())
                       .Parent.FullName; 
string target = @"DashBoard.html"; 
string finalPath = Path.Combine(path, target); 
System.Diagnostics.Process.Start(finalPath);

I have checked one of the solutions in that question and i understand it but what i can't find is if there is a way that it will work with all browsers.

Any hint?

Upvotes: 3

Views: 5832

Answers (2)

user3060735
user3060735

Reputation: 61

I was able to find a solution, this is the function i've created

private void RefreshPage()
{
    int count = 0;
    IntPtr[] explorerHandle = new IntPtr[4] { 
        FindWindow(null, "DashBoard - Google Chrome"), 
        FindWindow(null, "DashBoard - Internet Explorer"),
        FindWindow(null, "DashBoard - Opera"), 
        FindWindow(null, "DashBoard - Firefox")
    };

    foreach (IntPtr value in explorerHandle)
    {
        if (value != IntPtr.Zero)
        {
            count++;
            SetForegroundWindow(value);
            SendKeys.Send("{F5}");
        }
    }
    if(count == 0)
        System.Diagnostics.Process.Start("http://localhost:9000/DashBoard.html");
}

Thanks for your help

Upvotes: 1

Dave Bish
Dave Bish

Reputation: 19646

This is probably a bad idea, and you should look at trying to have the webpage refresh itself somehow.

Alternatively - you could look into the Chrome API - (assuming you only wish to target chrome?)

http://dev.chromium.org/developers

As a last resort - you could always kill the existing browser process - and open a new one:

var process = System.Diagnostics.Process.Start(finalPath);

//When refresh wanted:
process.Close();
//open again
var process = System.Diagnostics.Process.Start(finalPath);

Upvotes: 2

Related Questions