需求 Requirements
- 網頁瀏覽的物件:填寫表單之類的
- 執行外部程式:註冊完成後執行KKBOX
方案 Solutions
- Object: WebBrowser
- (Document) WebBrowser.Document
- (HtmlElementCollection) WebBrowser.Document.All
- (HtmlElement).GetAttribute
- (HtmlElement).SetAttribute
- Class: Microsoft.Win32 .Registry & .RegistryKey
- Class: Process
範例 Examples
// 瀏覽網頁
wb1.Navigate(@"https://ssl.kkbox.com.tw/reg_step1.php");
// 操作網頁元素
// 1 取得網頁物件
HtmlDocument hd = wb1.Document;
// 2 取得單一網頁元素
HtmlElement he = hd.All[0];
// HtmlElement he = hd.GetElementById("uid");
// 3 設定網頁元素的屬性
he.SetAttribute("name", "xxxx");
he.SetAttribute("checked", "false"); // for checkbox
he.InnerText = "yyyy";
// 4 取得網頁元素的屬性
string name = he.GetAttribute("name"); // return "xxxx"
// 5 執行網頁元素的事件
he.InvokeMember("click");
// 更新網頁
wb1.Refresh();
// 取得註冊表的值
string reg_key = "HKEY_CURRENT_USER\\Software\\Skysoft\\kkbox";
string path = Registry.GetValue(kkbox_reg, "path", "").ToString(); // return "C:\kkbox"
// 設定註冊表的值
Registry.SetValue(reg_key , "account", OMail, RegistryValueKind.String);
Registry.SetValue(kkbox_reg, "bRunKKTrayAtStartup", "0", RegistryValueKind.DWord);
// 刪除單一註冊表的Key
RegistryKey rk = Registry.CurrentUser;
rk.DeleteSubKey("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\KKBOX");
// 執行外部程式
Process kkbox = new Process();
kkbox.StartInfo.FileName = path + "\\KKBOX.exe";
kkbox.Start();
wb1.Navigate(@"https://ssl.kkbox.com.tw/reg_step1.php");
// 操作網頁元素
// 1 取得網頁物件
HtmlDocument hd = wb1.Document;
// 2 取得單一網頁元素
HtmlElement he = hd.All[0];
// HtmlElement he = hd.GetElementById("uid");
// 3 設定網頁元素的屬性
he.SetAttribute("name", "xxxx");
he.SetAttribute("checked", "false"); // for checkbox
he.InnerText = "yyyy";
// 4 取得網頁元素的屬性
string name = he.GetAttribute("name"); // return "xxxx"
// 5 執行網頁元素的事件
he.InvokeMember("click");
// 更新網頁
wb1.Refresh();
// 取得註冊表的值
string reg_key = "HKEY_CURRENT_USER\\Software\\Skysoft\\kkbox";
string path = Registry.GetValue(kkbox_reg, "path", "").ToString(); // return "C:\kkbox"
// 設定註冊表的值
Registry.SetValue(reg_key , "account", OMail, RegistryValueKind.String);
Registry.SetValue(kkbox_reg, "bRunKKTrayAtStartup", "0", RegistryValueKind.DWord);
// 刪除單一註冊表的Key
RegistryKey rk = Registry.CurrentUser;
rk.DeleteSubKey("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\KKBOX");
// 執行外部程式
Process kkbox = new Process();
kkbox.StartInfo.FileName = path + "\\KKBOX.exe";
kkbox.Start();
留言
張貼留言