一:用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的時候,不放在包含中文的路徑中還能夠,但一出現中文就掛了!
困擾我好半天,才發現的這個細節.