从网络浏览器获取网址

时间:2009-05-05 07:31:25

标签: c#

如何从当前在计算机上运行的Web浏览器中检索列表框的URL。使用c#

3 个答案:

答案 0 :(得分:1)

虽然不是一个完整的例子,但它显示了如何检索IE实例的地址栏中的文本: http://www.improve.dk/blog/2007/04/03/getting-text-from-handle

在检索文本之前,您需要获取该窗口的句柄。

答案 1 :(得分:1)

请参阅此question,其中包含c ++,但它可能有所帮助

答案 2 :(得分:0)

    [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;
    }

请执行以下操作以运行此代码 添加参考> Com>来自项目中VS.NET的Microsoft.Internet.Controls

http://ndde.codeplex.com/下载适用于DdeClient类的bin并将其添加到项目中