在.Net如何制作自定义的快捷方式(转)
生活随笔
收集整理的這篇文章主要介紹了
在.Net如何制作自定义的快捷方式(转)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們用.Net安裝程序生成的快捷方式是這樣的,如下圖:
? 該圖中目標(biāo)所對(duì)應(yīng)的文本框是灰色的,并且下方的查找目標(biāo)和更改圖標(biāo)兩個(gè)按鈕也是不可用。這樣我們根本就沒有辦法更改這個(gè)快捷方式。
假如這時(shí)有個(gè)客戶需要在程序啟動(dòng)的時(shí)候傳入一些參數(shù),那樣我們根本就沒有辦法,因?yàn)榭旖莘绞讲豢删庉?#xff0c;我們總不能讓客戶在CMD窗口啟動(dòng)吧~~這樣我們就不能使用.Net提供的快捷方式。只能是自己建立快捷方式。
那我們?cè)趺唇⒖旖莘绞侥?#xff0c;這里我們需要用到一個(gè)Com組件:Windows Script Host Object Model
這個(gè)組件,就是幫助我們建立快捷方式的。
首先:我們先在啟動(dòng)項(xiàng)目中添加上引用,如下圖
然后,我們?cè)僭趩?dòng)項(xiàng)目中添加一個(gè)安裝程序類,這個(gè)類的主要作用就是在程序進(jìn)行安裝和卸載的時(shí)候添加或者刪除快捷方式。代碼如下:
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Configuration.Install;
using?IWshRuntimeLibrary;
using?System.IO;
namespace?New
{
????[RunInstaller(true)]
????public?partial?class?MyInstaller?:?Installer
????{
????????public?MyInstaller()
????????{
????????????InitializeComponent();
????????}
????????public?override?void?Install(System.Collections.IDictionary?stateSaver)
????????{
????????????try
????????????{
????????????????base.Install(stateSaver);
????????????????System.Reflection.Assembly?Asm?=?System.Reflection.Assembly.GetExecutingAssembly();//獲取當(dāng)前程序集信息
????????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);//獲取當(dāng)前程序集位置
????????????????string?dbpath?=?fileinfo.DirectoryName;//獲取文件夾名稱
????????????????string?name?=?fileinfo.Name;//獲取文件名稱
????????????????//去掉后綴
????????????????if?(name.ToUpper().Contains(".EXE"))
????????????????{
????????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????????}
????????????????//在桌面創(chuàng)建快捷方式
????????????????WshShell?shell?=?new?WshShell();
????????????????IWshShortcut?shortcut?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut.TargetPath?=?Asm.Location;//目標(biāo)
????????????????shortcut.WorkingDirectory?=?dbpath;//工作文件夾
????????????????shortcut.WindowStyle?=?1;//窗體的樣式:1為默認(rèn),2為最大化,3為最小化
????????????????shortcut.Description?=?"yangyang8848";//快捷方式的描述
????????????????shortcut.IconLocation?=?Asm.Location;//圖標(biāo)
????????????????shortcut.Save();
????????????????//在程序菜單中創(chuàng)建文件夾
????????????????if?(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????????{
????????????????????Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name);
????????????????}
????????????????//在程序菜單中創(chuàng)建快捷方式
????????????????IWshShortcut?shortcut2?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut2.TargetPath?=?Asm.Location;
????????????????shortcut2.WorkingDirectory?=?dbpath;
????????????????shortcut2.WindowStyle?=?1;
????????????????shortcut2.Description?=?"yangyang8848"? ?"-"? ?name;
????????????????shortcut2.IconLocation?=?Asm.Location;
????????????????shortcut2.Save();
????????????}
????????????catch?(Exception?e)
????????????{
????????????????System.Windows.Forms.MessageBox.Show(e.Message);
????????????}
????????}
????????public?override?void?Uninstall(System.Collections.IDictionary?savedState)
????????{
????????????base.Uninstall(savedState);
????????????//卸載程序的時(shí)候?qū)蓚€(gè)快捷方式刪除
????????????System.Reflection.Assembly?Asm?=?System.Reflection.Assembly.GetExecutingAssembly();
????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);
????????????string?name?=?fileinfo.Name;
????????????if?(name.ToUpper().Contains(".EXE"))
????????????{
????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????}
????????????if?(Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????{
????????????????if?(Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\").Length?>?1)
????????????????{
????????????????????Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name? "\\",?true);
????????????????}
????????????????else
????????????????{
????????????????????Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\",?true);
????????????????}
????????????}
????????????if?(System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"))
????????????{
????????????????System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk");
????????????????
????????????}
????????}
????}
}
? 該圖中目標(biāo)所對(duì)應(yīng)的文本框是灰色的,并且下方的查找目標(biāo)和更改圖標(biāo)兩個(gè)按鈕也是不可用。這樣我們根本就沒有辦法更改這個(gè)快捷方式。
假如這時(shí)有個(gè)客戶需要在程序啟動(dòng)的時(shí)候傳入一些參數(shù),那樣我們根本就沒有辦法,因?yàn)榭旖莘绞讲豢删庉?#xff0c;我們總不能讓客戶在CMD窗口啟動(dòng)吧~~這樣我們就不能使用.Net提供的快捷方式。只能是自己建立快捷方式。
那我們?cè)趺唇⒖旖莘绞侥?#xff0c;這里我們需要用到一個(gè)Com組件:Windows Script Host Object Model
這個(gè)組件,就是幫助我們建立快捷方式的。
首先:我們先在啟動(dòng)項(xiàng)目中添加上引用,如下圖
然后,我們?cè)僭趩?dòng)項(xiàng)目中添加一個(gè)安裝程序類,這個(gè)類的主要作用就是在程序進(jìn)行安裝和卸載的時(shí)候添加或者刪除快捷方式。代碼如下:
?
using?System;using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Configuration.Install;
using?IWshRuntimeLibrary;
using?System.IO;
namespace?New
{
????[RunInstaller(true)]
????public?partial?class?MyInstaller?:?Installer
????{
????????public?MyInstaller()
????????{
????????????InitializeComponent();
????????}
????????public?override?void?Install(System.Collections.IDictionary?stateSaver)
????????{
????????????try
????????????{
????????????????base.Install(stateSaver);
????????????????System.Reflection.Assembly?Asm?=?System.Reflection.Assembly.GetExecutingAssembly();//獲取當(dāng)前程序集信息
????????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);//獲取當(dāng)前程序集位置
????????????????string?dbpath?=?fileinfo.DirectoryName;//獲取文件夾名稱
????????????????string?name?=?fileinfo.Name;//獲取文件名稱
????????????????//去掉后綴
????????????????if?(name.ToUpper().Contains(".EXE"))
????????????????{
????????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????????}
????????????????//在桌面創(chuàng)建快捷方式
????????????????WshShell?shell?=?new?WshShell();
????????????????IWshShortcut?shortcut?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut.TargetPath?=?Asm.Location;//目標(biāo)
????????????????shortcut.WorkingDirectory?=?dbpath;//工作文件夾
????????????????shortcut.WindowStyle?=?1;//窗體的樣式:1為默認(rèn),2為最大化,3為最小化
????????????????shortcut.Description?=?"yangyang8848";//快捷方式的描述
????????????????shortcut.IconLocation?=?Asm.Location;//圖標(biāo)
????????????????shortcut.Save();
????????????????//在程序菜單中創(chuàng)建文件夾
????????????????if?(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????????{
????????????????????Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name);
????????????????}
????????????????//在程序菜單中創(chuàng)建快捷方式
????????????????IWshShortcut?shortcut2?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut2.TargetPath?=?Asm.Location;
????????????????shortcut2.WorkingDirectory?=?dbpath;
????????????????shortcut2.WindowStyle?=?1;
????????????????shortcut2.Description?=?"yangyang8848"? ?"-"? ?name;
????????????????shortcut2.IconLocation?=?Asm.Location;
????????????????shortcut2.Save();
????????????}
????????????catch?(Exception?e)
????????????{
????????????????System.Windows.Forms.MessageBox.Show(e.Message);
????????????}
????????}
????????public?override?void?Uninstall(System.Collections.IDictionary?savedState)
????????{
????????????base.Uninstall(savedState);
????????????//卸載程序的時(shí)候?qū)蓚€(gè)快捷方式刪除
????????????System.Reflection.Assembly?Asm?=?System.Reflection.Assembly.GetExecutingAssembly();
????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);
????????????string?name?=?fileinfo.Name;
????????????if?(name.ToUpper().Contains(".EXE"))
????????????{
????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????}
????????????if?(Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????{
????????????????if?(Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\").Length?>?1)
????????????????{
????????????????????Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name? "\\",?true);
????????????????}
????????????????else
????????????????{
????????????????????Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\",?true);
????????????????}
????????????}
????????????if?(System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"))
????????????{
????????????????System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk");
????????????????
????????????}
????????}
????}
}
利用上邊的代碼創(chuàng)建出來的快捷方式樣式如下:
我們可以看到,這個(gè)快捷方式目標(biāo)處的文本框是可以編輯的,并且按鈕查找目標(biāo)和更改圖標(biāo)也是可以編輯的。這樣我們就可以在啟動(dòng)程序的時(shí)候通過快捷方式輸出參數(shù),滿足用戶的需求。
轉(zhuǎn)載于:https://www.cnblogs.com/yuanermen/archive/2007/10/07/916447.html
總結(jié)
以上是生活随笔為你收集整理的在.Net如何制作自定义的快捷方式(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WinForm给ComboBox增加Va
- 下一篇: IMP出现的ORA-01401错误可能和