2009-01-10 164 views
6

如何使用.NET 2.0窗口/控制檯應用程序從運行的Firefox實例獲取URL? C#或VB代碼將做。獲取Firefox網址?

謝謝!

+0

你需要更清楚一點。你的意思是如何從windows/console應用程序中運行的firefox實例獲取url? – Kev 2009-01-10 06:18:05

+0

是的......我編輯了這個問題。謝謝! – 2009-01-10 06:24:12

+0

如果這可以完成,您會意識到可能有多個實例,每個實例都有多個選項卡。你想達到什麼目的? – Kev 2009-01-10 06:26:22

回答

1

您可能要檢查到華廷的源代碼。他們的下一個版本是開源的,並且支持firefox,所以我會想象這樣做的功能。

+0

我編輯了問題,以添加更多的細節。我正在做一個winforms應用程序,我需要獲取瀏覽器的URL。我已經有了IE Url的代碼。謝謝! – 2009-01-10 06:17:34

4

對於大多數瀏覽器,包括IE瀏覽器,導航器,火狐和Opera,這樣做是爲了use DDE的支持和認可的方式。主題名稱全部爲WWW_GetWindowInfo;只有目標窗口的名稱不同。但是,這種技術對你來說很難,因爲.Net不支持DDE。如果你能找到一種方法來解決這個限制,你就會全部設置好。

1

窮人的解決方案,如果有的話都失敗了:啓動Firefox窗口,發送Ctrl + L鍵(激活地址欄),發送按Ctrl + C(複製選擇,即URL,複製到剪貼板。)和讀取剪貼板。

用這種方法的問題(他們當中確實奇怪玩意爲用戶,如果他們在電腦前),所以它只是一個備份解決方案,很多... ...

6

大廈羅伯肯尼迪的答案,並用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; 
     } 
} 

注意:這是非常緩慢的。我的電腦需要幾秒鐘的時間。結果將如下所示:

"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow","" 

有關更多信息DDE here

1
[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> Microsoft.Internet.Controls從VS.NET

http://ndde.codeplex.com/下載倉爲DdeClient類,並把它添加到您的項目

請讓我知道如果任何問題