C# 获取火狐网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/430614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Firefox URL?
提问by Leon Tayson
How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.
如何使用 .NET 2.0 windows/console 应用程序从正在运行的 firefox 实例获取 url?C# 或 VB 代码都可以。
Thanks!
谢谢!
回答by Jeff Martin
You may want to check into the source code of WatiN. Their next version is open source and supports firefox, so I would imagine the functionality for doing this is in it.
您可能需要查看 WatiN 的源代码。他们的下一个版本是开源的并支持 Firefox,所以我想这样做的功能就在其中。
回答by John Boker
it seems that this might be difficult, here's some discussion on it: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/
似乎这可能很难,这里有一些讨论:http: //social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/
回答by Rob Kennedy
For most browsers, including Internet Explorer, Navigator, Firefox, and Opera, the supported and sanctioned way of doing this is to use DDE. The topic name in all of them is WWW_GetWindowInfo
; only the name of the target window varies. That technique will be difficult for you, though, because .Net doesn't support DDE. If you can find a way to get around that limitation, you'll be all set.
对于大多数浏览器,包括 Internet Explorer、Navigator、Firefox 和 Opera,支持和认可的执行此操作的方法是使用 DDE。它们中的主题名称都是WWW_GetWindowInfo
; 只有目标窗口的名称不同。但是,该技术对您来说会很困难,因为 .Net 不支持 DDE。如果您能找到解决该限制的方法,那么您就大功告成了。
回答by PhiLho
Poor man's solution, if anything else fails: activate the Firefox window, send Ctrl+L (activates address bar), send Ctrl+C (copy selection, ie. URL, to clipboard) and read the clipboard.
可怜的人的解决方案,如果其他任何事情都失败了:激活 Firefox 窗口,发送 Ctrl+L(激活地址栏),发送 Ctrl+C(复制选择,即 URL,到剪贴板)并阅读剪贴板。
Lot of issues with this method (among them it does strange stuff for the user if they are in front of the computer) so it is only a backup solution...
这种方法有很多问题(其中,如果用户在电脑前,它会为用户做奇怪的事情)所以它只是一个备份解决方案......
回答by Foole
Building on Rob Kennedy's answer and using NDde
基于 Rob Kennedy 的回答并使用NDde
using NDde.Client;
class Test
{
public static string GetFirefoxURL()
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
dde.Disconnect();
return url;
}
}
NB: This is very slow. It takes a few seconds on my computer. The result will look something like this :
注意:这很慢。在我的电脑上需要几秒钟。结果将如下所示:
"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""
More info on browser DDE here.
有关浏览器 DDE 的更多信息,请访问此处。
回答by Serj-Tm
Use MozRepl: https://github.com/bard/mozrepl/wiki/+ mozRepl .NET Connector: http://mozreplconnector.codeplex.com/releases/view/17398
使用 MozRepl:https: //github.com/bard/mozrepl/wiki/+ mozRepl .NET 连接器:http: //mozreplconnector.codeplex.com/releases/view/17398
var connect = new MozReplConnectDotNet.MozReplConnect(4242);
connect.Connect();
Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
回答by Vijay Parmar
[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("##代码##","");
}
url = temp;
return temp;
}
Please do following to run this code Add Reference > Com > Microsoft.Internet.Controls from VS.NET in your project
请执行以下操作以在您的项目中从 VS.NET 添加参考 > Com > Microsoft.Internet.Controls
Download the bin from http://ndde.codeplex.com/for DdeClient class and add it to your project
从http://ndde.codeplex.com/下载DdeClient 类的 bin并将其添加到您的项目中
Please Let me know if any issue
如果有任何问题,请告诉我