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

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

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

在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. 【javascript】关于 canvas.toDataURL()

    在工作中遇到了奇怪的问题,在此记录. 一.定义 canvas.toDataURL()方法是返回一个包含图片展示的 数据URL.可以使用 type 参数其类型,默认为 PNG格式,图片的分辨率为96dp ...

  2. 修改密码 MVC

    控制器site public function actionPassword(){ $model = new PasswordForm(); /*判断请求属性 if ($request->isA ...

  3. [Unity移动端]真机调试

    一.Android Studio 1.log打印 打开AS,新建一个工程,点击左下角的Logcat,如下图,1是设备(支持模拟器,如果是真机的话,需要usb连接电脑,真机要是usb调试模式),2是包名 ...

  4. VC9.0 for Python2

    python2安转包时提示: pip install xxx时,提示 Microsoft Visual C++ 9.0 is required 下载并安装: 链接:https://pan.baidu. ...

  5. delphi中判断IP地址输入的合法性

    function GetSubStrCount(sSubStr, sParentStr: string): integer; begin Result := 0; while Pos(UpperCas ...

  6. Old ST-LINK firmware detected.do you want to upgrade it?升级固件

    一段时间没有使用st-link烧写程序了.打开keil5准备把程序烧写到stm32板子上时,报错Old ST-LINK firmware detected.do you want to upgrade ...

  7. MFC 与 C++ 类型转换

    C++ 中到的类型转换很多,先记录下来,多了写成一个类 1.CString转int 1 CString cNum="9527"; 2 int iNum = 0; 3 iNum=at ...

  8. odoo 权限管理学习总结

    环境 odoo-14.0.post20221212.tar base_user_role-12.0.2.1.2.zip 下载地址: https://apps.odoo.com/apps/modules ...

  9. 关于如何在IDEA里面配置好Git和Github的相关步骤详解

    配置Git 1.File->settings 然后选择其中的Version Control: 接着选择其中的Git选项: 2.将文本框内容定位到本地下载git路径下面 然后点击右边的Test测试 ...

  10. Android笔记--修改Device File Explorer的文件打开方式

    在首次打开该文件时,不小心选错了打开方式,导致以后每次打开也是同样的打开方式,也不会弹出第一次那样的打开方式的选择弹窗 在这里提供修改文件的默认打开方式的方法: 首先通过File->settin ...