[C#]使用動態資料交換 (DDE) 取得 FireFox 當前網址

  • 8788
  • 0

[C#]使用動態資料交換 (DDE) 取得 FireFox 當前網址

 

一、簡介

本文說明如何透過動態資料交換 (DDE) 取得 FireFox 當前網址。

動態資料交換(DDE) 是一種通訊協定,用來在 Microsoft Windows 程式之間交換資料。在 .NET 中,我們可以透過 NDde library 撰寫 DDE 功能。

 

二、方法

1. NDde 下載與加入參考

(1) 連結到 NDde 進行下載。

image

(2) 下載並且解壓縮後,可以看到包含 dll 檔案、說明文件、範例程式碼、NDde 原始碼。

image

(3) 開啟專案,將位於 Binary 下的 NDde.dll 加入參考。

SNAGHTML10b8c3 image

 

2. 取得 FireFox 網址

請參考以下程式碼與註解說明


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NDde.Client;

namespace WinFormGetFireFoxURL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetURL_Click(object sender, EventArgs e)
        {
            this.txtURL.Clear();
            this.txtURL.Text = GetFirefoxURL();
        }

        public static string GetFirefoxURL()
        {
            string sUrl = string.Empty;

            // 初始化 DdeClint 類別物件 ddeClient 
            // DdeClint(Server 名稱,string topic 名稱) 
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            // DDE Client 進行連結 
            dde.Connect();
            // 取得 URL 資訊
            string sUrlInfo = dde.Request("URL", int.MaxValue);
            // DDE Client 進行連結 
            dde.Disconnect();

            // 取得的 sUrlInfo 內容為 "網址","標題",""
            // 取出網址部分
            if (sUrlInfo.Length > 0)
            {
                sUrl = sUrlInfo.Split(',')[0].Trim(new char[]{'"'});
            }
            return sUrl;
        } 
    }
}

3. 執行結果

image

4. 範例下載

WinFormGetFireFoxURL.zip

 

三、相關參考與連結

[Office][C#]使用動態資料交換 (DDE) 將 EXCEL 檔案輸出至印表機進行列印