昨天想實現一個小功能,就是把正在瀏覽的某網頁添加到收藏夾中。以前在頁面直接用javascript調用一個方法就搞定了,現在我是想用winform來實現,我自己找了一下沒有看到相關的方法(可能找的不仔細)。于是想了一下決定自己實現算了。
完成這個功能主要是兩步,首先要取得系統用戶的收藏夾目錄,第二是要根據獲得頁面地址在收藏夾目錄創建一個快捷方式。
要獲得收藏加目錄我們可以用getfolderpath方法來完成,代碼如下
1string path=environment.getfolderpath(system.environment.specialfolder.favorites);要創建快捷方式需要用到iwshruntimelibrary命名空間,在這里我們要using一下。并在引用里添加一個com,windows script host object model。添加到收藏夾方法如下“
1public void addfavorites(string url,string filename,string savepath)
2 {
3 string path=environment.getfolderpath(system.environment.specialfolder.favorites);
4 if(!system.io.file.exists(path+"//"+filename+savepath+".url"))
5 {
6
7 iwshshell_class shell = new iwshshell_classclass();
8 iwshurlshortcut shortcut=null;
9 if(savepath=="favorites")
10 {
11 shortcut = shell.createshortcut(environment.getfolderpath(system.environment.specialfolder.favorites)+"//"+filename+".url") as iwshurlshortcut;
12 }
13 else
14 {
15 shortcut = shell.createshortcut(environment.getfolderpath(system.environment.specialfolder.favorites)+"//"+savepath+"http://"+filename+".url") as iwshurlshortcut;
16 }
17
18 shortcut.targetpath = url;
19 shortcut.save();
20 }
21 }
其中url是你要保存網頁的路徑,filename是生成快捷方式的名稱,savepath是在收藏夾中保存在哪個目錄。
順便想提個問題,有誰知道如何得到axwebbrowser對象中statustext。
新聞熱點
疑難解答