Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#应用程序刷新浏览器中已打开的选项卡_C#_Visual Studio 2010 - Fatal编程技术网

使用c#应用程序刷新浏览器中已打开的选项卡

使用c#应用程序刷新浏览器中已打开的选项卡,c#,visual-studio-2010,C#,Visual Studio 2010,我正在我的应用程序中打开一个html文件,如 string browserPath = GetBrowserPath(); Process process = new Process(); process.StartInfo = new ProcessStartInfo(browserPath); process.StartInfo.Arguments = "\"" + path_htmlFile + "\""; process.Start(); 但是如果我再次调用它,我只会在浏览器中获得一个

我正在我的应用程序中打开一个html文件,如

string browserPath = GetBrowserPath();
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = "\"" + path_htmlFile + "\"";
process.Start();
但是如果我再次调用它,我只会在浏览器中获得一个新选项卡,而不会刷新第一个选项卡。 我怎样才能解决这个问题


我正在使用c#with visual studio 2010

您可以激活浏览器应用程序,然后向其发送刷新键。实现这一点的一种方法是使用Windows API函数和方法


或者,您可以使用Windows脚本主机的WshShellObject.AppActivate和WshShellObject.SendKeys方法来实现这一点。请参阅。

刷新工作正常,但如何识别文件是否已打开?我找到了类似“if(process.MainWindowTitle.Contains(“html title”)”的方法,但这仅在文件在当前选项卡中打开且命令“SetForegroundWindow(process.MainWindowHandle”);时才起作用刷新所有选项卡,而不仅仅是所需的选项卡。本文列出了在浏览器中打开URL的一些方法。您可以通过比较这些URL来检查您的URL是否已经打开,并激活匹配的URL。
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);
}