C#net打包时自定义应用程序的快捷方式与卸载.
来源:互联网 发布:怎么使用photoshop软件 编辑:程序博客网 时间:2024/05/21 12:43
C#.net打包时自定义应用程序的快捷方式与卸载
一 自定义快捷方式(开始程序与桌面)
在你所要创建快捷方式的项目里面加上一个安装程序类(添加一个重要引用using IWshRuntimeLibrary;在com 里面windowns script host object model)
程序类的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using IWshRuntimeLibrary;
using System.IO;
namespace Win32
{
[RunInstaller(true)]
public partial class Myinstall : Installer
{
public Myinstall()
{
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", "");
name = name.Substring(0, 1) + name.Substring(1, 6).ToLower() + " " + name.Substring(8, 3);
}
//在桌面创建快捷方式
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 = "Aaa";//快捷方式的描述
shortcut.IconLocation = Asm.Location;//图标
shortcut.Save();
//在程序菜单中创建文件夹
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"))
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//");
}
//在程序菜单中创建快捷方式
IWshShortcut shortcut2 = (IWshShortcut)shell.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"+ "//" + name + ".lnk"
);
shortcut2.TargetPath = Asm.Location;
shortcut2.WorkingDirectory = dbpath;
shortcut2.WindowStyle = 1;
shortcut2.Description = "Aaa" + "-" + 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", "");
name = name.Substring(0, 1) + name.Substring(1, 6).ToLower() + " " + name.Substring(8, 3);
}
if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"))
{
if (Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//").Length > 1)
{
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//" + "//", true);
}
else
{
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//", true);
}
}
if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + name + ".lnk"))
{
System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + name + ".lnk");
}
}
}
}
然后在你的安装项目里面的自定义操作(安装项目--视图---自定义操作)里面加上该项目的主输出
“安装”与“卸载”都加上,如图
二 卸载 在上面的同一目录下添加开始程序快捷方式
1 卸载项目的创建
解决方案—添加新建项目---控制台应用程序---program 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace Uninst
{
class Program
{
static void Main(string[] args)
{
string sysroot = System.Environment.SystemDirectory;
System.Diagnostics.Process.Start(sysroot + "//msiexec.exe", "/x{9C3DE35E-BDAB-47E5-A52F-3BDB1DA276EA}");
}
}
}
2 在该项目中添加安装程序类 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using IWshRuntimeLibrary;
namespace Uninst// 卸载项目
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
public Installer1()
{
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();
//在程序菜单中创建文件夹
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"))
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//");
}
//在程序菜单中创建快捷卸载的方式
IWshShortcut shortcut2 = (IWshShortcut)shell.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"+ "//" + name + ".lnk"
);
shortcut2.TargetPath = Asm.Location;
shortcut2.WorkingDirectory = dbpath;
shortcut2.WindowStyle = 1;
shortcut2.Description = "卸载Aaa" + "-" + 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) + "//Aaa//"))
{
if (Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//").Length > 1)
{
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//"+ "//", true);
}
else
{
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//Aaa//", true);
}
}
if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + name + ".lnk"))
{
System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + name + ".lnk");
}
}
}
}
然后在你的安装项目里面的自定义操作(安装项目--视图---自定义操作)里面加上该项目的主输出
“安装”与“卸载”都加上,如图
三 生成安装包 安装 一切搞定
- C#net打包时自定义应用程序的快捷方式与卸载.
- C#.net打包时自定义应用程序的快捷方式与卸载
- C#.net打包时自定义应用程序的快捷方式与卸载
- .Net程序打包安装与卸载制作 ——怎样创建网页快捷方式(“开始与程序”菜单中)?(七)
- HOWTO:InstallShield中如何制作应用程序的卸载快捷方式
- C#.Net实现应用程序卸载(制作)
- Net程序打包安装与卸载制作
- Net程序打包安装与卸载制作
- 为VS.NET的部署项目添加卸载快捷方式
- 应用程序的打包与发布
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署图解
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署图解
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署图解
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署图解
- .net打包/C#WinFrom程序打包/图解.net打包/.net安装部署图解/VS2008项目的安装与部署图解
- 应用程序快捷方式的安全隐患
- Android 应用程序的快捷方式
- C# 对常用正则表达式封装
- Spring?的OpenSessionInViewFilter简介
- Sudoku逻辑
- 身份证号,出生日期等的js正则表达式验证
- VS2005安装部署项目时注册表项获得执行文件的目录(安装时设定开机自动运行)
- C#net打包时自定义应用程序的快捷方式与卸载.
- 《大话设计模式》--笔记(策略模式)
- Symbian判断是否是待机界面
- ExternalDataEventArgs
- 家电产品下乡 营销思路也该入乡随俗
- 至业内同行的一封信(反百度霸权主义)
- Brooks法则
- 达芬奇DM644X平台(ARM9, Linux-2.6.10)BSP之gpio.c浅析
- HandleExternalEventActivity