C# 创建快捷方式
C#兩種創建快捷方式的方法
用WSH直接創建快捷方式:?1.首先要添加引用.?
添加引用的方法非常簡單,右擊你的項目并選擇添加引用,?
選擇 COM 選項卡并選擇 Windows Script Host Object Model?
2.引用命名空間?
using System.Runtime.InteropServices;//互動服務?
using IWshRuntimeLibrary;?
3.創建快捷方式(注釋中有詳細說明)?
//實例化WshShell對象?
WshShell shell = new WshShell();?
//通過該對象的 CreateShortcut 方法來創建 IWshShortcut 接口的實例對象?
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(?
????Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//ShortCut.lnk");?
//設置快捷方式的目標所在的位置(源程序完整路徑)?
shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;?
//應用程序的工作目錄?
//當用戶沒有指定一個具體的目錄時,快捷方式的目標應用程序將使用該屬性所指定的目錄來裝載或保存文件。?
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;?
//目標應用程序窗口類型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)?
shortcut.WindowStyle = 1;?
//快捷方式的描述?
shortcut.Description = "ChinaDforce YanMang";?
//可以自定義快捷方式圖標.(如果不設置,則將默認源文件圖標.)?
//shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";?
//設置應用程序的啟動參數(如果應用程序支持的話)?
//shortcut.Arguments = "/myword /d4s";?
//設置快捷鍵(如果有必要的話.)?
//shortcut.Hotkey = "CTRL+ALT+D";?
//保存快捷方式?
shortcut.Save();?
缺點:?
用這種方法寫的程序,必須有Interop.IWshRuntimeLibrary.dll跟著,?
才能正確執行.對于創建"單文件程序"的人來講,麻煩了吧.?
通過創建VBS,并執行,創建方式:?
1.首先看一下VBS創建快捷方式的代碼:?
'VBS實例?
set WshShell = WScript.CreateObject("WScript.Shell")?
strDesktop = WshShell.SpecialFolders("Desktop") '獲得桌面目錄?
set oShellLink = WshShell.CreateShortcut(strDesktop & "\D4S.lnk") '快捷方式存放目錄及名稱?
oShellLink.TargetPath = "X:\Program Files\XXX.exe"?? '指向的可執行文件?
oShellLink.WindowStyle = 1 '運行方式(窗體打開的方式)?
oShellLink.Hotkey = "CTRL+SHIFT+F"????'快捷鍵?
oShellLink.IconLocation = "X:\Program Files\XXX.exe, 0" '圖標(同樣可不指定)?
oShellLink.Description = "ChinaDforce YanMang"????'備注信息?
oShellLink.WorkingDirectory = "X:\Program Files\"?? '起始目錄?
oShellLink.Save '保存快捷方式?
2.那我們如何在C#中使用VBS呢??
方法我想應該有很多吧!?
在這里介紹一種"最笨"但最直接的方法.?
思路如下:?
>>> 生成VBS全部代碼文本;?
>>> 寫入臨時文件"temp.vbs";?
>>> 用Process打開這個文件執行.?
3.下面是C#中實現的關鍵代碼:?
//生成VBS代碼?
string vbs = this.CreateVBS();?
//以文件形式寫入臨時文件夾?
this.WriteToTemp(vbs);?
//調用Process執行?
this.RunProcess();?
//生成VBS代碼?
string vbs = this.CreateVBS();?
//以文件形式寫入臨時文件夾?
this.WriteToTemp(vbs);?
//調用Process執行?
this.RunProcess();?
///?
/// 創建VBS代碼?
///?
///?
private string CreateVBS()?
{?
????string vbs = string.Empty;?
????vbs += ("set WshShell = WScript.CreateObject(\"WScript.Shell\")\r\n");?
????vbs += ("strDesktop = WshShell.SpecialFolders(\"Desktop\")\r\n");?
????vbs += ("set oShellLink = WshShell.CreateShortcut(strDesktop & \"\\D4S.lnk\")\r\n");?
????vbs += ("oShellLink.TargetPath = \"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"\r\n");?
????vbs += ("oShellLink.WindowStyle = 1\r\n");?
????vbs += ("oShellLink.Description = \"ChinaDforce YanMang\"\r\n");?
????vbs += ("oShellLink.WorkingDirectory = \"" + System.Environment.CurrentDirectory + "\"\r\n");?
????vbs += ("oShellLink.Save");?
????return vbs;?
}?
///?
/// 寫入臨時文件?
///?
///?
private void WriteToTemp(string vbs)?
{?
????if (!string.IsNullOrEmpty(vbs))?
????{?
????????//臨時文件?
????????string tempFile = Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "[url=file://\\temp.vbs]\\temp.vbs[/url]";?
????????//寫入文件?
????????FileStream fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write);?
????????try?
????????{?
????????????//這里必須用UnicodeEncoding. 因為用UTF-8或ASCII會造成VBS亂碼?
????????????System.Text.UnicodeEncoding uni = new UnicodeEncoding();?
????????????byte[] b = uni.GetBytes(vbs);?
????????????fs.Write(b, 0, b.Length);?
????????????fs.Flush();?
????????????fs.Close();?
????????}?
????????catch (Exception ex)?
????????{?
????????????MessageBox.Show(ex.Message, "寫入臨時文件時出現錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);?
????????}?
????????finally?
????????{?
????????????//釋放資源?
????????????fs.Dispose();?
????????}?
????}?
}?
///?
/// 執行VBS中的代碼?
///?
private void RunProcess()?
{?
????string tempFile = Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "\\temp.vbs";?
????if (File.Exists(tempFile))?
????{?
????????//執行VBS?
????????Process.Start(tempFile);?
????}?
}?
private void btn退出_Click(object sender, EventArgs e)?
{?
????Application.Exit();?
????//清除臨時文件?
????File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "\\temp.vbs");?
}?
強調一點:?
在寫入VBS文件時,一定要用UnicodeEncoding.?
因為UTF-8和ASCII碼,都會導致VBS生成快捷方式的時候,?
產生亂碼,而導致快捷方式錯誤.?
本人原來使用UTF8Encoding的時候,不放在包含中文的路徑中還可以,但一出現中文就掛了!?
困擾我好半天,才發現的這個細節.
源:http://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html
?
轉載于:https://www.cnblogs.com/08shiyan/archive/2011/06/15/2081216.html
總結
- 上一篇: 成都房屋过户咨询哪个部门
- 下一篇: 局域网(LocalAreaNetwork