在C#windows窗体应用程序中获取浏览器的活动URL

时间:2018-06-18 13:02:28

标签: c# visual-studio winforms firefox

我创建了一个窗体表单应用程序。 此应用程序获取浏览器的活动URL并将其保存到文本文件中。 这在 chrome &的 IE

但是当我使用 firefox 时,这将无效。此代码无法获取 firefox 浏览器的活动网址。

我不知道为什么会这样。

我使用以下代码查找网址

 public string GetBrowsedUrl()
        {

            IntPtr hwnd = APIFuncs.getforegroundWindow();
            Int32 pid = APIFuncs.GetWindowProcessID(hwnd);
            Process process = Process.GetProcessById(pid);
            string appId = proc.Id.ToString();
            string appName = proc.ProcessName;
            string appltitle = APIFuncs.ActiveApplTitle().Trim().Replace("\0", "");
            if (process == null)
                throw new ArgumentNullException("process");

            if (process.MainWindowHandle == IntPtr.Zero)
                return null;

            AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
            if (element == null)
                return null;

            AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));


            string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            return result;
        }

2 个答案:

答案 0 :(得分:0)

你也不能将此代码用于firefox。

我建议使用名为NDde的第三方库来轻松完成此操作。 Here is NDde link

    public string GetFirefoxUrl()
    {
        try
        {
            Process[] pname = Process.GetProcessesByName("Firefox");
            if (pname.Length != 0)
            {
                DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
                dde.Connect();
                string url = dde.Request("URL", int.MaxValue);
                url= url.Replace("\"", "").Replace("\0", "");
                dde.Disconnect();
                return url;
            }
            else
                return null;
        }
        catch
        {

            return null;
        }
    }

答案 1 :(得分:0)

最后我找到了答案

public string GetBrowsedUrl(Process process)
{
    if (process.ProcessName == "firefox")
    {
        if (process == null)
            throw new ArgumentNullException("process");

        if (process.MainWindowHandle == IntPtr.Zero)
            return null;

        AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
        if (element == null)
            return null;

        AutomationElement doc = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
        if (doc == null)
            return null;

        return ((ValuePattern)doc.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
    }
    else
    {
        if (process == null)
            throw new ArgumentNullException("process");

        if (process.MainWindowHandle == IntPtr.Zero)
            return null;

        AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
        if (element == null)
            return null;

        AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));


        string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
        return result;
    }

}