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");
            }
        }
    }
}
然后在你的安装项目里面的自定义操作(安装项目--视图---自定义操作)里面加上该项目的主输出
“安装”与“卸载”都加上,如图
 

 

三 生成安装包  安装  一切搞定

原创粉丝点击