从.Net WinForms应用程序以编程方式刷新浏览器的页面

从asp.net页面,通过ClickOnce部署,启动.Net WinForms应用程序。 在某个时刻,WinForm应用程序需要刷新它启动的网页。

我怎么能这样做? 基于.Net的Windows应用程序如何刷新已在浏览器中打开的页面?

以强大的方式做这件事并不容易。 例如,用户可能没有使用IE。

您控制的唯一内容是网页和Windows应用程序通用的是您的Web服务器。

这个解决方案很复杂,但这是我能想到的唯一可行的方法。

1)在Windows应用程序运行之前,获取网页以打开与Web服务器的长轮询连接。 SignalR此刻正在为此做好准备。

2)获取Windows应用程序,以便在想要更新网页时向服务器发送信号。

3)在服务器上,完成长轮询请求,将信号发送回Web浏览器。

4)在网页中,通过刷新页面来处理响应。

我说它很复杂!

这里有一些示例代码可以满足您的需求(只是相关部分):

 using System.Runtime.InteropServices; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { // Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); private void RefreshExplorer() { //You may want to receive the window caption as a parameter... //hard-coded for now. // Get a handle to the current instance of IE based on window title. // Using Google as an example - Window caption when one navigates to google.com IntPtr explorerHandle = FindWindow("IEFrame", "Google - Windows Internet Explorer"); // Verify that we found the Window. if (explorerHandle == IntPtr.Zero) { MessageBox.Show("Didn't find an instance of IE"); return; } SetForegroundWindow(explorerHandle ); //Refresh the page SendKeys.Send("{F5}"); //The page will refresh. } } } 

注意:该代码是此MSDN示例的修改。