正规软件建议还是使用官方的标准安装程序组件,因为官方的标准安装/卸载组件能更好的与操作系统衔接,安装和卸载流程更加规范。

今天提供一种野路子,全用代码实现安装卸载器。

需要写一个程序,包含安装器、卸载器、主程序。

在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#自行实现安装卸载程序(不使用官方组件)的更多相关文章

  1. WPF 自己动手来做安装卸载程序

    原文:WPF 自己动手来做安装卸载程序 前言 说起安装程序,这也许是大家比较遗忘的部分,那么做C/S是小伙伴们,难道你们的程序真的不需要一个炫酷的安装程序么? 声明在先 本文旨在教大家以自己的方式实现 ...

  2. 帮同事写了几行代码,在 安装/卸载 程序里 注册/卸载 OCX控件

    写了个小控制台程序,这个程序用来注册 / 卸载OCX控件,用在Inno Setup做的安装卸载程序里. #include "stdafx.h" #include <windo ...

  3. 使用Powershell实现自动化安装/卸载程序

    最近需要制作软件安装包,需要附带VC运行时和.Net Framework的安装,但又不想让用户自己点下一步,所以就有了以下操作. 微软提供了一个程序叫msiexec.exe,位于C:\Windows\ ...

  4. 用Setup系列函数完成驱动卸载安装[驱动安装卸载程序]

    // InstallWDFDriver.cpp : Defines the entry point for the console application. // #include "std ...

  5. centos7 安装卸载程序rpm使用方法

    1.安装 rpm 包: ➢ 基本语法 rpm -ivh RPM 包全路径名称 2.卸载 rpm 包: ➢ 基本语法 rpm -e RPM 包的名称 ➢ 应用案例 删除 firefox 软件包 rpm ...

  6. Linux如何安装卸载软件

    Linux 中如何卸载已安装的软件. Linux软件的安装和卸载一直是困扰许多新用户的难题.在Windows中,我们可以使用软件自带的安装卸载程序或在控制面板中的“添加/删除程 序” 来实现.与其相类 ...

  7. Android 安装 卸载 更新 程序

    安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...

  8. inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效

    inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...

  9. Inno Setup 在安装程序开始前和卸载程序开始前,检查并关闭运行的进程

    (2011-12-29 11:54:56) 转载▼ 标签: innosetup it 分类: 开发工具经验累积 Inno Setup在安装程序前,如果有使用的进程在运行,会有错误提示,而使得Insta ...

  10. InnoSetup打包exe安装应用程序,并添加卸载图标 转

    http://blog.csdn.net/guoquanyou/article/details/7445773 InnoSetup真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...

随机推荐

  1. 微信支付服务商api对接

    引入官方sdk <!--微信v3支付sdk {https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient}--> < ...

  2. 2021SWPUCTF-WEB(三)

    error ​ 双引号没有提示的注入,,那就是报错注入了,肯定是个恶心的东西呜呜呜 ?id=1' and updatexml(1,concat(0x7e,(select right(flag,30) ...

  3. java中取数组第一个元素

    java中取数组第一个元素   var a=[1,2,2,3,4];console.log(a);a.shift();console.log(a);   pop:删除原数组最后一项,并返回删除元素的值 ...

  4. 使用redis分布式锁重复执行采坑

    事件:生产环境部署两台,每天凌晨1点,定时任务同步更新(先删除,后全部插入)账号表,使用了redis分布式锁,发现定时任务还是执行了两次,导致数据重复,影响对应业务. 原因分析:定时任务执行的逻辑是调 ...

  5. 几行代码,把zip文件直接破解

    几行代码,把zip文件直接破解,不想讲解了,如果要使用就直接复制吧,讲解挺累的 import itertools import zipfile import os filename = "z ...

  6. boost编译指定python版本号

    1.执行如下 bootstrap 语句,会在目录下生成 project-config.jam 文件 .\bootstrap --with-python="C:\Users\yzy\Anaco ...

  7. VMwareWorkstation-安装虚拟机

    安装vmware 首先就是下载VMware客户端了,Vmware是收费的,过好大多数都有破解版,或者激活码 这里我是用的是VMware16,下载在网上搜一下就有,例如 下载解压后里面有一个后缀为exe ...

  8. 14.AQS的前世,从1990年的论文说起

    大家好,我是王有志.关注王有志,一起聊技术,聊游戏,聊在外漂泊的生活. 鸽了这么久怪不好意思的,因此送一本<多处理器编程的艺术>,快点击此处参加吧.另外欢迎大家加入"共同富裕的J ...

  9. 【Keil】浅学一下keil中的.sct文件

    [Keil]浅学一下keil中的.sct文件 最近重新捣鼓了acfly的源码,有了新的有趣发现,当然,过程并不有趣. 起因 clone下来我去年提交的代码,编译 ...... 报错辣! linking ...

  10. 音视频编解码流程与如何使用 FFMPEG 命令进行音视频处理

    一.前言 FFMPEG 是特别强大的专门用于处理音视频的开源库.你既可以使用它的 API 对音视频进行处理,也可以使用它提供的工具,如 ffmpeg, ffplay, ffprobe,来编辑你的音视频 ...