使用C#获取Chrome、Firefox和IE中的当前URL,可以通过以下方式实现:
System.Diagnostics
和System.Runtime.InteropServices
命名空间。Process.GetProcessesByName
方法获取所有Chrome进程的进程对象数组。Process.MainWindowHandle
属性获取Chrome窗口的句柄。GetWindowText
函数和StringBuilder
类获取Chrome窗口的标题。示例代码:
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
// 获取Chrome中的当前URL
public string GetChromeCurrentURL()
{
Process[] chromeProcesses = Process.GetProcessesByName("chrome");
foreach (Process process in chromeProcesses)
{
IntPtr handle = process.MainWindowHandle;
StringBuilder title = new StringBuilder(256);
GetWindowText(handle, title, title.Capacity);
string titleString = title.ToString();
if (titleString != "")
{
// 解析标题字符串,获取URL
// 示例代码省略
return currentURL;
}
}
return null;
}
// 调用Windows API函数获取窗口标题
DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
System.IO
和Microsoft.Win32
命名空间。RegistryKey
类读取Firefox的安装路径。Process.Start
方法启动Firefox,并通过命令行参数-osint -url "%u"
打开一个新标签页。Process.WaitForInputIdle
方法等待Firefox进程就绪。SendKeys
类发送快捷键Ctrl+L
和Ctrl+C
,将当前URL复制到剪贴板。Clipboard
类获取剪贴板中的文本,即为当前URL。示例代码:
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Forms;
// 获取Firefox中的当前URL
public string GetFirefoxCurrentURL()
{
string firefoxPath = GetFirefoxInstallationPath();
if (firefoxPath != null)
{
Process process = new Process();
process.StartInfo.FileName = firefoxPath;
process.StartInfo.Arguments = "-osint -url \"%u\"";
process.Start();
process.WaitForInputIdle();
SendKeys.SendWait("^l");
SendKeys.SendWait("^c");
process.WaitForExit();
string currentURL = Clipboard.GetText();
return currentURL;
}
return null;
}
// 获取Firefox的安装路径
private string GetFirefoxInstallationPath()
{
string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(uninstallKey))
{
foreach (string subKeyName in key.GetSubKeyNames())
{
using (RegistryKey subKey = key.OpenSubKey(subKeyName))
{
string displayName = subKey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains("Firefox"))
{
string installLocation = subKey.GetValue("InstallLocation") as string;
if (installLocation != null)
{
string firefoxPath = Path.Combine(installLocation, "firefox.exe");
if (File.Exists(firefoxPath))
{
return firefoxPath;
}
}
}
}
}
}
return null;
}
System.Runtime.InteropServices
命名空间。SHDocVw.ShellWindows
类获取所有正在运行的IE窗口对象。InternetExplorer
接口的HWND
属性获取IE窗口的句柄。GetWindowText
函数和StringBuilder
类获取IE窗口的标题。示例代码:
using System.Runtime.InteropServices;
using System.Text;
// 获取IE中的当前URL
public string GetIECurrentURL()
{
ShellWindows shellWindows = new ShellWindows();
foreach (InternetExplorer window in shellWindows)
{
IntPtr handle = (IntPtr)window.HWND;
StringBuilder title = new StringBuilder(256);
GetWindowText(handle, title, title.Capacity);
string titleString = title.ToString();
if (titleString != "")
{
// 解析标题字符串,获取URL
// 示例代码省略
return currentURL;
}
}
return null;
}
// 调用Windows API函数获取窗口标题
DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
以上是使用C#获取Chrome、Firefox和IE中的当前URL的方法。请注意,由于浏览器的版本和配置可能会有所不同,以上代码可能需要根据实际情况进行适当的调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云