c#制作类似qq安装程序一样的单文件程序安装包
程序员文章站
2024-02-24 22:51:28
复制代码 代码如下:using system;using system.collections.generic;using system.componentmodel;...
复制代码 代码如下:
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();
}
}
}