c#制作类似qq安装程序一样的单文件程序安装包
发布时间:2021-07-22 17:13:31
标签:程序安装包,qq安装程序,单文件程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.IO;
using System.IO.Compression;
using System.Resources;
using System.Net;
using System.Web.Services.Description;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace MON.Client
{
public partial class MainForm : Form
{
bool testFlag = false;
Dictionary<string, string> dic;
Thread t;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
dic = new Dictionary<string, string>();
groupBox1.Visible = true;
groupBox2.Visible = false;
}
/// <summary>
/// 安装路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InstallPathBTN_Click(object sender, EventArgs e)
{
DialogResult dr = FolerBrowserCreator.ShowDialog();
if (DialogResult.OK == dr)
{
InstallPathTB.Text = FolerBrowserCreator.SelectedPath;
if (dic.ContainsKey("installPath"))
{
dic["installPath"] = InstallPathTB.Text;
}
else
{
dic.Add("installPath", InstallPathTB.Text);
}
if (string.IsNullOrEmpty(LogInstallPahtTB.Text))
{
LogInstallPahtTB.Text = Path.Combine(InstallPathTB.Text, "log");
if (dic.ContainsKey("logPath"))
{
dic["logPath"] = LogInstallPahtTB.Text;
}
else
{
dic.Add("logPath", LogInstallPahtTB.Text);
}
}
}
}
/// <summary>
/// 日志路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LogPathBrowseBtn_Click(object sender, EventArgs e)
{
DialogResult dr = FolerBrowserCreator.ShowDialog();
if (DialogResult.OK == dr)
{
LogInstallPahtTB.Text = FolerBrowserCreator.SelectedPath;
if (dic.ContainsKey("logPath"))
{
dic["logPath"] = LogInstallPahtTB.Text;
}
else
{
dic.Add("logPath", LogInstallPahtTB.Text);
}
}
}
/// <summary>
/// 测试webservice;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void testWebServiceBTN_Click(object sender, EventArgs e)
{
testWebServiceBTN.Enabled = false;
TestService();
if (testFlag)
{
MessageBox.Show("测试通过", "系统提示", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("网站地址有误", "系统提示", MessageBoxButtons.OK);
}
testWebServiceBTN.Enabled = true;
}
/// <summary>
/// 测试webservice
/// </summary>
void TestService()
{
WebClient wc = new WebClient();
Stream stream1 = null;
Stream stream2 = null;
try
{
var url = WebSiteTB.Text.Trim().ToUpper();
if (!url.EndsWith("/MON/MONSERVICE.ASMX"))
{
url = url.TrimEnd('/') + "/MON/MONSERVICE.ASMX";
}
if (dic.ContainsKey("webService"))
{
dic["webService"] = url;
}
else
{
dic.Add("webService", url);
}
stream1 = wc.OpenRead(url + "?op=GetMachineConfig");
stream2 = wc.OpenRead(url + "?op=UpdateServerStatus");
if (stream1.CanRead && stream2.CanRead)
{
testFlag = true;
}
}
catch
{
testFlag = false;
}
finally
{
wc.Dispose();
if (stream1 != null)
{
stream1.Close();
}
if (stream2 != null)
{
stream2.Close();
}
}
}
/// <summary>
/// 开始安装
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartBtn_Click(object sender, EventArgs e)
{
if (!Directory.Exists(InstallPathTB.Text.Trim()))
{
MessageBox.Show("安装路径有误", "系统提示", MessageBoxButtons.OK);
return;
}
if (LogInstallPahtTB.Text.Trim().StartsWith(InstallPathTB.Text.Trim()))
{
if (!Directory.Exists(LogInstallPahtTB.Text.Trim()))
{
Directory.CreateDirectory(LogInstallPahtTB.Text.Trim());
}
}
else
{
if (!Directory.Exists(LogInstallPahtTB.Text.Trim()))
{
MessageBox.Show("日志路径有误", "系统提示", MessageBoxButtons.OK);
return;
}
}
if (testFlag == false)
{
TestService();//test过就不用再test一次了
}
if (testFlag == false)
{
MessageBox.Show("网站地址有误", "系统提示", MessageBoxButtons.OK);
return;
}
try
{
int days = Convert.ToInt32(DaysTB.Text.Trim());
if (days < 1)
{
throw new Exception();
}
}
catch
{
MessageBox.Show("日志保存天数有误", "系统提示", MessageBoxButtons.OK);
return;
}
dic.Add("logDays", DaysTB.Text.Trim());
groupBox1.Visible = false;
groupBox2.Visible = true;
InstallInfoTB.Text = "开始安装";
t = new Thread(new ThreadStart(InstallJob));
t.Start();
}
/// <summary>
/// 安装线程
/// </summary>
void InstallJob()
{
WriteLine("准备安装环境...");
var configPath = Path.Combine(dic["installPath"], "MON.WS.exe.config");
var exePath = configPath.TrimEnd(".config".ToCharArray());
var args = new List<string>();
args.Add(@"net stop 服务器性能监控UTRY");
args.Add(@"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u " + exePath);
args.Add("exit");
if (!cmdCommand(args))
{
WriteLine("在卸载原有服务时出现异常");
WriteLine("安装失败");
return;
}
WriteLine("释放配置文件...");
if (!releaseConfig(dic, configPath))
{
WriteLine("释放配置文件过程中出现异常");
WriteLine("安装失败");
return;
}
WriteLine("配置文件释放完毕...");
WriteLine("释放可执行文件...");
if (!releaseExe(exePath))
{
WriteLine("释放可执行文件时出现异常");
WriteLine("安装失败");
return;
}
WriteLine("可执行文件释放完毕...");
WriteLine("开始安装服务...");
args.Clear();
args.Add(@"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe " + exePath);
args.Add(@"net start 服务器性能监控UTRY");
args.Add("exit");
if (!cmdCommand(args))
{
WriteLine("在安装和启动服务时出现异常");
WriteLine("安装失败");
return;
}
WriteLine("服务安装成功");
WriteLine("安装成功");
}
/// <summary>
/// 释放exe
/// </summary>
/// <param name="exePath"></param>
bool releaseExe(string exePath)
{
try
{
var data = Properties.Resources.MON_WS;
if (File.Exists(exePath))
{
File.Delete(exePath);
}
var f = new FileStream(exePath, FileMode.Create);
f.Write(data, 0, data.Length);
f.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 释放Config
/// </summary>
/// <param name="dic"></param>
/// <param name="configPath"></param>
bool releaseConfig(Dictionary<string, string> dic, string configPath)
{
try
{
var configStr = Properties.Resources.MON_WS_exe;
WriteLine("配置相关信息...");
configStr = configStr.Replace("#WebServiceUrl#", dic["webService"]);
configStr = configStr.Replace("#LogSavePath#", dic["logPath"]);
configStr = configStr.Replace("#LogSaveDays#", dic["logDays"]);
if (File.Exists(configPath))
{
File.Delete(configPath);
}
StreamWriter sw = File.AppendText(configPath);
sw.Write(configStr);
sw.Flush();
sw.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 执行CMD命令
/// </summary>
/// <param name="args"></param>
bool cmdCommand(List<string> args)
{
try
{
var process = new Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
foreach (var arg in args)
{
process.StandardInput.WriteLine(arg);
}
process.WaitForExit();
//var result = process.StandardOutput.ReadToEnd();
process.Close();
return true;
}
catch
{
return false;
}
}
delegate void mydele(string text);
/// <summary>
/// 更新安装信息
/// </summary>
/// <param name="text"></param>
void WriteLine(string text)
{
if (InstallInfoTB.InvokeRequired)
{
mydele dd = new mydele(WriteLine);
InstallInfoTB.BeginInvoke(dd, new object[] { text });
}
else
{
InstallInfoTB.Text = InstallInfoTB.Text + Environment.NewLine + text;
if (text == "安装成功"||text == "安装失败")
{
CompleteBTN.Enabled = true;
}
}
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CancelBTN_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// 完成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CompleteBTN_Click(object sender, EventArgs e)
{
if (t != null)
{
t.Abort();
t.Join();
}
this.Close();
}
}
}


猜你喜欢
- 本文实例为大家分享了android使用OPENGL ES绘制圆柱体的具体代码,供大家参考,具体内容如下效果图:编写jiem.java&nbs
- 1、首先看一下下面两个sql语句的区别:<select id="selectByNameAndPassword"
- 功能:通过响应UI Textbox 的值向ListView 绑定新添加的纪录。UI XAML 代码<Grid Margin="
- 我们在安卓开发中安卓自带的控件满足不了我们的需求,因此我们就需要用到自定义View来满足我们的需求,在这里我要讲解的是自定义View实现选座
- 加载本地图片在项目目录下创建assets文件夹,再在其文件夹下创建images文件夹,后面将需要的图片复制到其中即可在pubspec.yam
- 本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下1、TextView 主要用于界面上显示一段文本
- Java多态对象的类型转换这里所说的对象类型转换,是指存在继承关系的对象,不是任意类型的对象。当对不存在继承关系的对象进行强制类型转换时,j
- 报错翻译: compileSdkVersion android-24”需要JDK 1.8或更高版本编译。报错现象如下图:原因:st
- 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Thre
- 在新建Java项目时,run运行main方法时,报错 “java: 错误: 无效的源发行版:16”,
- 之前百度,google了很多,发现并没有介绍mongodb生产环境如何配置的文章, 当时想参考下都不行, 所以写篇文章,大家可以一块讨论下.
- 1 修饰属性或者变量无论属性是基本类型还是引用类型,作用都是变量里面存放的“值”不可变经常和static关键字一起使用,作为常量1 基本类型
- 前言有些业务比较复杂,比如我们需要新建10张表,每张表有10个字段,如果用手工来操作,肯定非常浪费时间,而且随着代码中对实体类的修改,还要同
- MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移
- 大多数android程序员应该都知道genymotion是一个不错的模拟器,体积小巧,启动速度快。相关的博客也比较多,但是一直以来无法解决a
- 1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个Serv
- 一、界面部分:首先,打开visual studio新建项目;然后使用“工具箱”添加控件:分别添加button,datagridview,te
- 1-:生成一个签名密钥你可以用keytool命令生成一个私有密钥。在Windows上keytool命令放在JDK的bin目录中(比如C:\P
- Swing组件中的事件处理专门用于响应用户的操作,例如,响应用户的鼠标单击、按下键盘等操作。在Swing事件处理的过程中,主要涉及三类对象:
- 通常在 java 中对文本、网络资源等操作起来是很繁杂的,要声明,读取,关闭三个阶段,还得考虑异常情况。假设我们要读取一段文本显示到控制台,