簡體   English   中英

使用ac#應用程序刷新瀏覽器中的選項卡(如果已打開)

[英]refresh Tab in Browser if already open, using a c# application

我正在像我的應用程序中打開一個html文件

string browserPath = GetBrowserPath();
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = "\"" + path_htmlFile + "\"";
process.Start();

但是,如果我再次調用它,我將在瀏覽器中僅得到一個新標簽,而不刷新第一個標簽。 我該如何解決?

我在Visual Studio 2010中使用C#

您可以激活瀏覽器應用程序,然后向其發送刷新密鑰。 實現此目的的一種方法是使用SetForegroundWindow Windows API函數和System.Windows.Forms.SendKeys.SendWait方法。

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class Program
{
    static void Main(string[] args)
    {
        var browserPath = @"chrome";
        RefreshBrowserTab(browserPath);
    }

    private static void RefreshBrowserTab(string browserPath)
    {
        var processName = Path.GetFileNameWithoutExtension(browserPath);
        var processes = Process.GetProcessesByName(processName);
        foreach (var process in processes)
        {
            SetForegroundWindow(process.MainWindowHandle);
        }
        SendKeys.SendWait("^r");
    }

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

另外,您可以使用Windows腳本宿主的WshShellObject.AppActivate和WshShellObject.SendKeys方法來實現。 請參閱http://msdn.microsoft.com/en-us/library/ahcz2kh6(v=vs.84).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM