在.Net如何制作自定义的快捷方式(转)
2008-04-14 10:03
381 查看
我们用.Net安装程序生成的快捷方式是这样的,如下图:
该图中目标所对应的文本框是灰色的,并且下方的查找目标和更改图标两个按钮也是不可用。这样我们根本就没有办法更改这个快捷方式。
假如这时有个客户需要在程序启动的时候传入一些参数,那样我们根本就没有办法,因为快捷方式不可编辑,我们总不能让客户在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");
}
}
}
}
利用上边的代码创建出来的快捷方式样式如下:
我们可以看到,这个快捷方式目标处的文本框是可以编辑的,并且按钮查找目标和更改图标也是可以编辑的。这样我们就可以在启动程序的时候通过快捷方式输出参数,满足用户的需求。
该图中目标所对应的文本框是灰色的,并且下方的查找目标和更改图标两个按钮也是不可用。这样我们根本就没有办法更改这个快捷方式。
假如这时有个客户需要在程序启动的时候传入一些参数,那样我们根本就没有办法,因为快捷方式不可编辑,我们总不能让客户在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");
}
}
}
}
利用上边的代码创建出来的快捷方式样式如下:
我们可以看到,这个快捷方式目标处的文本框是可以编辑的,并且按钮查找目标和更改图标也是可以编辑的。这样我们就可以在启动程序的时候通过快捷方式输出参数,满足用户的需求。
相关文章推荐
- .net制作安装包 如何生成快捷方式
- 如何制作一个网页快捷方式?不简单你杀了我。
- NSIS制作卸载器时注意事项和如何创建桌面快捷方式
- 如何用WinCE CabManager制作的cab文件,创建桌面快捷方式?
- InstallShield中如何制作应用程序的卸载快捷方式
- HOWTO:InstallShield中如何制作应用程序的卸载快捷方式
- 如何创建linux命令”快捷方式”或”自定义命令”
- <转>如何创建linux命令”快捷方式”或”自定义命令”
- 如何自定义命令提示符Command Prompt的快捷方式
- HOWTO:InstallShield中如何制作应用程序的卸载快捷方式
- 如何自定义命令提示符Command Prompt的快捷方式
- (转)类(class)和结构(struct)的区别是什么?它们对性能有影响吗?.NET BCL里有哪些是类(结构),为什么它们不是结构(类)?在自定义类型时,您如何选择是类还是结构?
- 如何去除桌面快捷方式小箭头
- 如何自定义报表动作的打开方式
- 如何去除桌面快捷方式的小箭头
- 如何恢复XP快捷方式的小箭头——最简单有效的方法!
- 如何在android桌面添加几个app图标做快捷方式?
- 在 .NET 中创建快捷方式 [C#][转载]
- .net创建快捷方式
- Hive表中四种不同数据导出方式以及如何自定义导出列分隔符