Reputation: 303
I am opening an html file in my application like
string browserPath = GetBrowserPath();
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = "\"" + path_htmlFile + "\"";
process.Start();
But if i call it again, i get only a new tab in my browser instead of refreshing the first one. How could i solve this?
I am using c# with visual studio 2010
Upvotes: 0
Views: 2401
Reputation: 168
You can activate the browser application followed by sending refresh keys to it. One way of achieving this is to use the SetForegroundWindow Windows API function and the System.Windows.Forms.SendKeys.SendWait method.
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);
}
Alternatively, you can achieve this using Windows Scripting Host's WshShellObject.AppActivate and WshShellObject.SendKeys methods. See http://msdn.microsoft.com/en-us/library/ahcz2kh6(v=vs.84).aspx.
Upvotes: 2