C#.net打包时自定义应用程序的快捷方式与卸载

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");

            }

        }

    }

}

然后在你的安装项目里面的自定义操作(安装项目--视图---自定义操作)里面加上该项目的主输出

“安装”与“卸载”都加上,如图

 

 

生成安装包  安装  一切搞定

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值