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真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...
随机推荐
- scrapy框架中的pipelines没有成功调用process_item方法
提示报错 原因: items没有接收到Spider的返回值,导致pipelines没有接收到items模块的返回值,检查Spider模块是否正确返回值,我这里的原因是,数据解析完成后没有yield i ...
- TCP连接实践解析
1.初始化. 2.FD_ISSET,是select机制的一个成员,用来检测sockfd是否有动作,对应读写异常等. 3.FD_ZERO 宏完成的工作就是一个初始化套接字集合 4.FD_SET把sock ...
- LeetCode92 反转链表Ⅱ
idea:参考上一道全部反转,所以反转链表部分代码实现,我觉得重点在于集中不同情况的分类讨论.一共四类情况需要考虑,有前有后,有前无后,有后无前,无前无后. /** * Definition for ...
- Git 仓库7K stars!学Java开源项目austin要多久?
我是3y,一年CRUD经验用十年的markdown程序员常年被誉为职业八股文选手 开源项目消息推送平台austin仓库地址: 消息推送平台推送下发[邮件][短信][微信服务号][微信小程序][企业微 ...
- 声网Agora 教育 aPaaS 灵动课堂升级:UI与业务逻辑分离,界面、功能自定义更灵活
声网Agora 教育 aPaaS 产品灵动课堂现已升级至 v1.1.0 版本.声网Agora 灵动课堂可以帮助教育机构和开发者最快 15 分钟上线自有品牌.全功能的在线互动教学平台,节省 90% 开发 ...
- GPSSworld仿真(一):程序题——单窗口排队系统
3.3 一个仓库共存放了2000吨货物,货物以三种规模出库,少量(10吨),中等(20吨),大量(50吨),分别以10±5分,15分,30±10分的速率出库.如果没有货位达到的情况下,一个仓库能维持供 ...
- 爬取JSON文件并且存储
思路 1 先调用模块 2 定义一个函数 2.1 获取网址(点击评论 找到JSON的文件(分析评论preview)获取Request URL后面的地址) 2.2 添加用户的请求头 2.3 使用get方法 ...
- Anconda、Pycharm下载、安装、配置教程(极其详细)
Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大( ...
- 一次对pool的误用导致的.net频繁gc的诊断分析
(最近有读者朋友表示,希望能加一些示意图来描述分析过程中用到的原理知识.好的,之后我会注意,谢谢这位读者) 背景 有位朋友找我,希望我能帮看一下他的一个service.从他的描述看,并没有资源方面的泄 ...
- 字符串算法--$\mathcal{KMP,Trie}$树
\(\mathcal{KMP算法}\) 实际上,完全没必要从\(S\)的每一个字符开始,暴力穷举每一种情况,\(Knuth.Morris\)和\(Pratt\)对该算法进行了改进,称为KMP算法. 而 ...