c#制作類似qq安裝程序一樣的單文件程序安裝包
更新時(shí)間:2014年01月11日 10:44:13 作者:
c#制作單文件安裝程序,可安裝windows服務(wù),類似安裝QQ,大家參考使用吧
復(fù)制代碼 代碼如下:
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>
/// 測(cè)試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("測(cè)試通過", "系統(tǒng)提示", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("網(wǎng)站地址有誤", "系統(tǒng)提示", MessageBoxButtons.OK);
}
testWebServiceBTN.Enabled = true;
}
/// <summary>
/// 測(cè)試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("安裝路徑有誤", "系統(tǒng)提示", 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("日志路徑有誤", "系統(tǒng)提示", MessageBoxButtons.OK);
return;
}
}
if (testFlag == false)
{
TestService();//test過就不用再test一次了
}
if (testFlag == false)
{
MessageBox.Show("網(wǎng)站地址有誤", "系統(tǒng)提示", MessageBoxButtons.OK);
return;
}
try
{
int days = Convert.ToInt32(DaysTB.Text.Trim());
if (days < 1)
{
throw new Exception();
}
}
catch
{
MessageBox.Show("日志保存天數(shù)有誤", "系統(tǒng)提示", 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("準(zhǔn)備安裝環(huán)境...");
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 服務(wù)器性能監(jiān)控UTRY");
args.Add(@"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u " + exePath);
args.Add("exit");
if (!cmdCommand(args))
{
WriteLine("在卸載原有服務(wù)時(shí)出現(xiàn)異常");
WriteLine("安裝失敗");
return;
}
WriteLine("釋放配置文件...");
if (!releaseConfig(dic, configPath))
{
WriteLine("釋放配置文件過程中出現(xiàn)異常");
WriteLine("安裝失敗");
return;
}
WriteLine("配置文件釋放完畢...");
WriteLine("釋放可執(zhí)行文件...");
if (!releaseExe(exePath))
{
WriteLine("釋放可執(zhí)行文件時(shí)出現(xiàn)異常");
WriteLine("安裝失敗");
return;
}
WriteLine("可執(zhí)行文件釋放完畢...");
WriteLine("開始安裝服務(wù)...");
args.Clear();
args.Add(@"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe " + exePath);
args.Add(@"net start 服務(wù)器性能監(jiān)控UTRY");
args.Add("exit");
if (!cmdCommand(args))
{
WriteLine("在安裝和啟動(dòng)服務(wù)時(shí)出現(xiàn)異常");
WriteLine("安裝失敗");
return;
}
WriteLine("服務(wù)安裝成功");
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("配置相關(guān)信息...");
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>
/// 執(zhí)行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();
}
}
}
相關(guān)文章
c#實(shí)現(xiàn)sunday算法實(shí)例
Sunday算法思想跟BM算法很相似,在匹配失敗時(shí)關(guān)注的是文本串中參加匹配的最末位字符的下一位字符,下面是用C#實(shí)現(xiàn)sunday的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析
這篇文章主要介紹了C#中事務(wù)處理和非事務(wù)處理方法,較為詳細(xì)的分析了C#中事務(wù)處理與非事務(wù)處理的使用技巧,對(duì)于使用C#進(jìn)行數(shù)據(jù)庫程序開發(fā)有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07c#使用UTF-8編碼實(shí)現(xiàn)處理多語言文本
UTF-8編碼是現(xiàn)代應(yīng)用中處理多語言文本的首選,所以本文為大家詳細(xì)介紹了C#如何使用UTF-8編碼實(shí)現(xiàn)處理多語言文本,感興趣的小伙伴可以了解下2024-01-01C#實(shí)體類轉(zhuǎn)換的兩種方式小結(jié)
這篇文章主要介紹了C#實(shí)體類轉(zhuǎn)換的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01C#中結(jié)構(gòu)體定義并轉(zhuǎn)換字節(jié)數(shù)組詳解
在寫C#TCP通信程序時(shí),發(fā)送數(shù)據(jù)時(shí),只能發(fā)送byte數(shù)組,處理起來比較麻煩不說,如果是和VC6.0等寫的程序通信的話,很多的都是傳送結(jié)構(gòu)體,在VC6.0中可以很方便的把一個(gè)char[]數(shù)組轉(zhuǎn)換為一個(gè)結(jié)構(gòu)體,而在C#卻不能直接把byte數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體,要在C#中發(fā)送結(jié)構(gòu)體,應(yīng)該怎么做呢?2017-11-11