Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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# 获取Firefox URL?_C#_.net_Firefox - Fatal编程技术网

C# 获取Firefox URL?

C# 获取Firefox URL?,c#,.net,firefox,C#,.net,Firefox,如何使用.NET 2.0 windows/console应用程序从运行中的firefox实例获取url?C#或VB代码就可以了 谢谢 您可能需要查看WatiN的源代码。他们的下一个版本是开源的,并且支持firefox,所以我可以想象这样做的功能就在它里面。看起来这可能很困难,这里有一些讨论:对于大多数浏览器,包括Internet Explorer、Navigator、firefox和Opera,支持和认可的方式是。它们中的主题名称都是WWW_GetWindowInfo;只有目标窗口的名称不同。不

如何使用.NET 2.0 windows/console应用程序从运行中的firefox实例获取url?C#或VB代码就可以了


谢谢

您可能需要查看WatiN的源代码。他们的下一个版本是开源的,并且支持firefox,所以我可以想象这样做的功能就在它里面。

看起来这可能很困难,这里有一些讨论:

对于大多数浏览器,包括Internet Explorer、Navigator、firefox和Opera,支持和认可的方式是。它们中的主题名称都是
WWW_GetWindowInfo
;只有目标窗口的名称不同。不过,这种技术对您来说很困难,因为.Net不支持DDE。如果你能找到绕过这个限制的方法,你就万事大吉了。

穷人的解决方案,如果其他任何方法都失败了:激活Firefox窗口,发送Ctrl+L(激活地址栏),发送Ctrl+C(复制选择,即URL,到剪贴板),然后读取剪贴板


这种方法存在很多问题(其中,如果用户站在计算机前,它会给用户带来奇怪的东西),因此它只是一种备份解决方案…

基于Rob Kennedy的答案并使用

注:这是非常缓慢的。在我的电脑上用几秒钟。结果如下所示:

"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""
有关浏览器DDE的详细信息。

使用MozRepl:+MozRepl.NET连接器:

请执行以下操作以运行此代码 在项目中从VS.NET添加引用>Com>Microsoft.Internet.Controls

从DdeClient类下载bin并将其添加到项目中

如果有任何问题,请告诉我,试试这个:

        //get all running process of firefox
        Process[] procsfirefox = Process.GetProcessesByName("firefox");
        foreach (Process firefox in procsfirefox)
        {
            //the firefox process must have a window
            if (firefox.MainWindowHandle == IntPtr.Zero)
            {
                continue;
            }
            AutomationElement sourceElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
                          
            AutomationElement editBox = sourceElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"));             
            // if it can be found, get the value from the editbox
            if (editBox != null)
            {
                ValuePattern val = ((ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern));

                Console.WriteLine("\n Firefox URL found: " + val.Current.Value);
            }

            //-----------------------------find titlebar element for site title---------------------------------// 
            
            AutomationElement elmTitleBar = sourceElement.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
            if (elmTitleBar != null)
            
                Console.WriteLine("\n Firefox TitleBar found: " + elmTitleBar.Current.Name);
            }

完整源代码:

我已经编辑了这个问题以添加更多细节。我正在做一个winforms应用程序,我需要获取浏览器URL。我已经有了IE Url的代码。谢谢你需要更清楚一点。你是说如何在windows/控制台应用程序中从运行的firefox实例获取url?是的。。。我已经编辑了这个问题。谢谢如果可以做到这一点,您会意识到可能会有多个实例,每个实例都有多个选项卡。你想实现什么?是的,我知道。我在IE上做过这个,事实上,我在firefox上做过这个,但是它使用的是.NET3.5类,但是客户端希望在.NET2.0中完成它。因为我不知道C。我会用自动热键来做…:-PI从几年前开始使用这段代码。然而,随着FF新版本的发布,它不再工作了。你知道有没有其他方法可以达到同样的效果?
  var connect = new MozReplConnectDotNet.MozReplConnect(4242);
  connect.Connect();
  Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr parentHandle,
    IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd,
        int msg, int wParam, StringBuilder ClassName);

    private static string GetURL(IntPtr intPtr, string programName, out string url)
    {
        string temp=null;
        if (programName.Equals("chrome"))
        {
            var hAddressBox = FindWindowEx(intPtr, IntPtr.Zero, "Chrome_OmniboxView", IntPtr.Zero);
            var sb = new StringBuilder(256);
            SendMessage(hAddressBox, 0x000D, (IntPtr)256, sb);
            temp = sb.ToString();
        } 
        if (programName.Equals("iexplore"))
        {
            foreach (InternetExplorer ie in new ShellWindows())
            {
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName);
                if (fileNameWithoutExtension != null)
                {
                    var filename = fileNameWithoutExtension.ToLower();
                    if (filename.Equals("iexplore"))
                    {
                        temp+=ie.LocationURL + " ";
                    }
                }
            }
        }
        if (programName.Equals("firefox"))
       {
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            dde.Connect();
            string url1 = dde.Request("URL", int.MaxValue);
            dde.Disconnect();
            temp = url1.Replace("\"","").Replace("\0","");
        }
        url = temp;
        return temp;
    }
        //get all running process of firefox
        Process[] procsfirefox = Process.GetProcessesByName("firefox");
        foreach (Process firefox in procsfirefox)
        {
            //the firefox process must have a window
            if (firefox.MainWindowHandle == IntPtr.Zero)
            {
                continue;
            }
            AutomationElement sourceElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
                          
            AutomationElement editBox = sourceElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"));             
            // if it can be found, get the value from the editbox
            if (editBox != null)
            {
                ValuePattern val = ((ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern));

                Console.WriteLine("\n Firefox URL found: " + val.Current.Value);
            }

            //-----------------------------find titlebar element for site title---------------------------------// 
            
            AutomationElement elmTitleBar = sourceElement.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
            if (elmTitleBar != null)
            
                Console.WriteLine("\n Firefox TitleBar found: " + elmTitleBar.Current.Name);
            }