C#自行实现安装卸载程序(不使用官方组件)
正规软件建议还是使用官方的标准安装程序组件,因为官方的标准安装/卸载组件能更好的与操作系统衔接,安装和卸载流程更加规范。
今天提供一种野路子,全用代码实现安装卸载器。
需要写一个程序,包含安装器、卸载器、主程序。
在visual studio中创建一个解决方案,解决方案里创建3个项目分别对应安装器、卸载器、主程序。
如图

制作安装包目录时,将三个项目全部生成可执行程序。然后按下方文件结构组织安装包,复制最终程序文件到相应位置。
U8FileTransferIntaller
+-- U8FileTransfer
| +-- main
| |-- U8FileTransfer.exe
| |-- ...
| +-- uninstall.exe
+-- intall.exe
* Installer生成install.exe,用于拷贝U8FileTransfer目录到用户选择的安装路劲,注册表添加开机自启,启动U8FileTransfer.exe
* UnInstaller生成uninstall.exe,用于卸载程序(退出主程序,取消开机自启,删除main目录)
* U8FileTransfer是主程序。
卸载时会删除main目录,uninstall.exe无法自己删除自己,需手动删除。
下面只讲安装和卸载器的实现,不讲主程序。
安装器

功能:复制目录及文件,注册表添加开启自启,启动程序,关闭自身
Intaller.cs 代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
// using System.Diagnostics; namespace Installer
{
public partial class Intaller : Form
{
private string appDirName = "U8FileTransfer"; public Intaller()
{
InitializeComponent();
} /// <summary>
/// 复制目录(包括子目录及所有文件)到另一个地方
/// </summary>
/// <param name="sourceDirName"></param>
/// <param name="destDirName"></param>
/// <param name="copySubDirs"></param>
private void directoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName); if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found:"
+ sourceDirName);
} DirectoryInfo[] dirs = dir.GetDirectories(); // If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDirName); // Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
file.CopyTo(tempPath, true);
} // If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string tempPath = Path.Combine(destDirName, subdir.Name);
directoryCopy(subdir.FullName, tempPath, copySubDirs);
}
}
} // 文件浏览按钮事件
private void folderBrowserButton_Click(object sender, EventArgs e)
{
DialogResult dr = folderBrowserDialog.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
folderPathTextBox.Text = folderBrowserDialog.SelectedPath + "\\" + appDirName;
} } // 确认按钮事件
private void okButton_Click(object sender, EventArgs e)
{
/**
* 1.复制目录及文件
*/
string sourceDirName = Application.StartupPath + "\\" + appDirName;
string destDirName = @folderPathTextBox.Text;
directoryCopy(sourceDirName, destDirName, true); /**
* 2.注册表添加开启自启
*/
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if(key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
key.SetValue(appDirName, destDirName + "\\main\\U8FileTransfer.exe");
key.Close(); /**
* 3.启动程序
*/
string start = @folderPathTextBox.Text + "\\main\\U8FileTransfer.exe";
System.Diagnostics.Process.Start(start); //关闭自身
Application.Exit();
} }
}
卸载器

功能:退出运行中的主程序,删除注册表开机自启项,删除安装目录,弹出提示,退出自身
Uninstall.cs 代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO; namespace Uninstaller
{
public partial class Uninstall : Form
{
public Uninstall()
{
InitializeComponent();
} private void cancelButton_Click(object sender, EventArgs e)
{
Application.Exit();
} private void confirmButton_Click(object sender, EventArgs e)
{
// 退出运行中的主程序
Process[] process = Process.GetProcesses();
foreach (Process prc in process)
{
// ProcessName为exe程序的名称,比如叫main.exe,那么ProcessName就为main
if (prc.ProcessName == "U8FileTransfer")
{
prc.Kill();
break;
}
} // 删除注册表开机自启项
// 打开注册表子项
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (key != null)
{
try
{
key.DeleteValue("U8FileTransfer");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
key.Close(); // 删除目录
DeleteDir(Application.StartupPath + "\\main"); // 弹出提示
MessageBox.Show("以卸载完成,Uninstall.exe需要手动删除"); // 退出自身
Application.Exit(); } /// <summary>
/// 删除文件夹
/// </summary>
/// <param name="file">需要删除的文件路径</param>
/// <returns></returns>
public bool DeleteDir(string file)
{
try
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(file))
{
foreach (string f in Directory.GetFileSystemEntries(file))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
//Console.WriteLine(f);
}
else
{
//循环递归删除子文件夹
DeleteDir(f);
}
}
//删除空文件夹
Directory.Delete(file);
}
return true;
}
catch (Exception) // 异常处理
{
return false;
} } }
}
C#自行实现安装卸载程序(不使用官方组件)的更多相关文章
- WPF 自己动手来做安装卸载程序
原文:WPF 自己动手来做安装卸载程序 前言 说起安装程序,这也许是大家比较遗忘的部分,那么做C/S是小伙伴们,难道你们的程序真的不需要一个炫酷的安装程序么? 声明在先 本文旨在教大家以自己的方式实现 ...
- 帮同事写了几行代码,在 安装/卸载 程序里 注册/卸载 OCX控件
写了个小控制台程序,这个程序用来注册 / 卸载OCX控件,用在Inno Setup做的安装卸载程序里. #include "stdafx.h" #include <windo ...
- 使用Powershell实现自动化安装/卸载程序
最近需要制作软件安装包,需要附带VC运行时和.Net Framework的安装,但又不想让用户自己点下一步,所以就有了以下操作. 微软提供了一个程序叫msiexec.exe,位于C:\Windows\ ...
- 用Setup系列函数完成驱动卸载安装[驱动安装卸载程序]
// InstallWDFDriver.cpp : Defines the entry point for the console application. // #include "std ...
- centos7 安装卸载程序rpm使用方法
1.安装 rpm 包: ➢ 基本语法 rpm -ivh RPM 包全路径名称 2.卸载 rpm 包: ➢ 基本语法 rpm -e RPM 包的名称 ➢ 应用案例 删除 firefox 软件包 rpm ...
- Linux如何安装卸载软件
Linux 中如何卸载已安装的软件. Linux软件的安装和卸载一直是困扰许多新用户的难题.在Windows中,我们可以使用软件自带的安装卸载程序或在控制面板中的“添加/删除程 序” 来实现.与其相类 ...
- Android 安装 卸载 更新 程序
安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...
- inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...
- Inno Setup 在安装程序开始前和卸载程序开始前,检查并关闭运行的进程
(2011-12-29 11:54:56) 转载▼ 标签: innosetup it 分类: 开发工具经验累积 Inno Setup在安装程序前,如果有使用的进程在运行,会有错误提示,而使得Insta ...
- InnoSetup打包exe安装应用程序,并添加卸载图标 转
http://blog.csdn.net/guoquanyou/article/details/7445773 InnoSetup真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...
随机推荐
- vue-devtools 打开 vscode 可能会报错
据说 99% 的人不知道 vue-devtools 还能直接打开对应组件文件?本文原理揭秘 mac 电脑在 VSCode command + shift + p,Windows 则是 ctrl + s ...
- shell多进程并发数控制
在批量执行任务时,单进程执行速度太慢,使用&不加数量控制,又担心资源占用过多,导致宕机等问题,因此我们需要控制并发进程的数量,保证效率的同时,保证资源占用不会太高. 其中一个解决思路是利用简单 ...
- STM32上特殊管脚应用 重映射那个GPIO_Remap_SWJ_JTAGDisable
对于初学习者来说为什么用到PB3和PB4时无法控制输出呢? 下面就这一问题进行分析讲解. 首先,STM32F10x系列的MCU复位后,PA13/14/15 & PB3/4默认配置为JTAG功能 ...
- Bootstarp5第二弹
四.网格系统 网格系统根据设备屏幕尺寸大小分为6类: col-<!--任意屏幕--> col-sm-<!--平板 - 屏幕宽度等于或大于 576px.--> col-md-&l ...
- K8S部署应用详解
# 前言 首先以SpringBoot应用为例介绍一下k8s的发布步骤. 1.从代码仓库下载代码,比如GitLab:2.接着是进行打包,比如使用Maven:3.编写Dockerfile文件,把步骤2产生 ...
- 实践解析丨如何通过 WebAssembly 在 Web 进行实时视频人像分割
5 月 15 日,声网Agora 高级架构师高纯参加了 WebAssambly 社区举办的第一场线下活动"WebAssembly Meetup",并围绕声网Agora 在 Web ...
- 被冰封的 Bug:Fishhook Crash 修复纪实
作者:郝连福,业界资深计算机技术专家,现任声网Agora 首席前端架构师.先后担任过 Principal Engineer/Engineering Director(UTStarcom).Sr. ar ...
- 【超详细】Ubuntu 20.04 安装 Apache+PHP网页环境 图文教程,常见问题和解决方案
本文将介绍在Ubuntu20.04 LTS环境下安装Apache的全过程,针对其中可能出现的一些坑也会提供解决方案. 作者:Eriktse 简介:19岁,211计算机在读,现役ACM银牌选手力争以通俗 ...
- 第四部分:Spdlog日志库的核心组件分析-logger
Spdlog是一个快速且可扩展的C++日志库,它支持多线程和异步日志记录.在本文中,我们将分析Spdlog日志库的核心代码,探究其实现原理和代码结构. Spdlog的基本架构 上一篇文章介绍了spdl ...
- JetBrains 2022全家桶-激活
## JetBrains 全家桶 激活教程 https://tech.souyunku.com/?page_id=50199