【小技能】C#寫入html文件實現連接有道詞典API

有道詞典提供了API,在網站中可以直接調用,所以可以C#寫入html文件實現連接有道詞典API

(1)自己寫一個網頁,可以直接搜索有道API,相關頁面有詳細說明;

(2)通過c#的webBrower調用網頁;

(3)獲取textBox中的輸入,寫入html文件

(4)網頁中即可使用有道詞典

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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            string tex = textBox1.Text;
            HtmlElement el = webBrowser1.Document.CreateElement("DIV");
            el.InnerHtml = "<span style=\"color:red\">" + tex + "</span>";
            webBrowser1.Document.Body.AppendChild(el);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string str = "file:///C:/Users/user/Desktop/webtest.html";//一定要注意改路徑!!!html文件的存放位置
                webBrowser1.Navigate(new Uri(str)); 	//打開網站
                //this.Text = str; //將打開的網址設置爲標題欄

            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message.ToString()); }
        }
    }
}

效果圖:


附上下載地址http://download.csdn.net/detail/gny315/8633649

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章