在.Net如何制作自定义的快捷方式(转)
生活随笔
收集整理的這篇文章主要介紹了
在.Net如何制作自定义的快捷方式(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們用.Net安裝程序生成的快捷方式是這樣的,如下圖:
? 該圖中目標所對應的文本框是灰色的,并且下方的查找目標和更改圖標兩個按鈕也是不可用。這樣我們根本就沒有辦法更改這個快捷方式。
假如這時有個客戶需要在程序啟動的時候傳入一些參數,那樣我們根本就沒有辦法,因為快捷方式不可編輯,我們總不能讓客戶在CMD窗口啟動吧~~這樣我們就不能使用.Net提供的快捷方式。只能是自己建立快捷方式。
那我們怎么建立快捷方式呢,這里我們需要用到一個Com組件:Windows Script Host Object Model
這個組件,就是幫助我們建立快捷方式的。
首先:我們先在啟動項目中添加上引用,如下圖
然后,我們再在啟動項目中添加一個安裝程序類,這個類的主要作用就是在程序進行安裝和卸載的時候添加或者刪除快捷方式。代碼如下:
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();//獲取當前程序集信息
????????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);//獲取當前程序集位置
????????????????string?dbpath?=?fileinfo.DirectoryName;//獲取文件夾名稱
????????????????string?name?=?fileinfo.Name;//獲取文件名稱
????????????????//去掉后綴
????????????????if?(name.ToUpper().Contains(".EXE"))
????????????????{
????????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????????}
????????????????//在桌面創建快捷方式
????????????????WshShell?shell?=?new?WshShell();
????????????????IWshShortcut?shortcut?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut.TargetPath?=?Asm.Location;//目標
????????????????shortcut.WorkingDirectory?=?dbpath;//工作文件夾
????????????????shortcut.WindowStyle?=?1;//窗體的樣式:1為默認,2為最大化,3為最小化
????????????????shortcut.Description?=?"yangyang8848";//快捷方式的描述
????????????????shortcut.IconLocation?=?Asm.Location;//圖標
????????????????shortcut.Save();
????????????????//在程序菜單中創建文件夾
????????????????if?(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????????{
????????????????????Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name);
????????????????}
????????????????//在程序菜單中創建快捷方式
????????????????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);
????????????//卸載程序的時候將兩個快捷方式刪除
????????????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");
????????????????
????????????}
????????}
????}
}
? 該圖中目標所對應的文本框是灰色的,并且下方的查找目標和更改圖標兩個按鈕也是不可用。這樣我們根本就沒有辦法更改這個快捷方式。
假如這時有個客戶需要在程序啟動的時候傳入一些參數,那樣我們根本就沒有辦法,因為快捷方式不可編輯,我們總不能讓客戶在CMD窗口啟動吧~~這樣我們就不能使用.Net提供的快捷方式。只能是自己建立快捷方式。
那我們怎么建立快捷方式呢,這里我們需要用到一個Com組件:Windows Script Host Object Model
這個組件,就是幫助我們建立快捷方式的。
首先:我們先在啟動項目中添加上引用,如下圖
然后,我們再在啟動項目中添加一個安裝程序類,這個類的主要作用就是在程序進行安裝和卸載的時候添加或者刪除快捷方式。代碼如下:
?
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();//獲取當前程序集信息
????????????????System.IO.FileInfo?fileinfo?=?new?System.IO.FileInfo(Asm.Location);//獲取當前程序集位置
????????????????string?dbpath?=?fileinfo.DirectoryName;//獲取文件夾名稱
????????????????string?name?=?fileinfo.Name;//獲取文件名稱
????????????????//去掉后綴
????????????????if?(name.ToUpper().Contains(".EXE"))
????????????????{
????????????????????name?=?name.ToUpper().Replace(".EXE",?"");
????????????????}
????????????????//在桌面創建快捷方式
????????????????WshShell?shell?=?new?WshShell();
????????????????IWshShortcut?shortcut?=?(IWshShortcut)shell.CreateShortcut(
????????????????????Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)? ?"\\"? ?name? ?".lnk"
????????????????????);
????????????????shortcut.TargetPath?=?Asm.Location;//目標
????????????????shortcut.WorkingDirectory?=?dbpath;//工作文件夾
????????????????shortcut.WindowStyle?=?1;//窗體的樣式:1為默認,2為最大化,3為最小化
????????????????shortcut.Description?=?"yangyang8848";//快捷方式的描述
????????????????shortcut.IconLocation?=?Asm.Location;//圖標
????????????????shortcut.Save();
????????????????//在程序菜單中創建文件夾
????????????????if?(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name))
????????????????{
????????????????????Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs)? ?"\\yangyang8848\\"? ?name);
????????????????}
????????????????//在程序菜單中創建快捷方式
????????????????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);
????????????//卸載程序的時候將兩個快捷方式刪除
????????????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");
????????????????
????????????}
????????}
????}
}
利用上邊的代碼創建出來的快捷方式樣式如下:
我們可以看到,這個快捷方式目標處的文本框是可以編輯的,并且按鈕查找目標和更改圖標也是可以編輯的。這樣我們就可以在啟動程序的時候通過快捷方式輸出參數,滿足用戶的需求。
轉載于:https://www.cnblogs.com/yuanermen/archive/2007/10/07/916447.html
總結
以上是生活随笔為你收集整理的在.Net如何制作自定义的快捷方式(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WinForm给ComboBox增加Va
- 下一篇: asp.net ajax控件工具集 Au