a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#制作類似qq安裝程序一樣的單文件程序安裝包

2020-01-24 02:55:38
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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();
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一本色道久久综合狠狠躁篇怎么玩 | 奇米精品一区二区三区在线观看 | 亚洲精品一区二区三区中文字幕 | 国产黄色免费网站 | 国产在线精品一区 | 男人的天堂久久 | 欧美一区二区三区免费 | 亚洲v日韩v综合v精品v | 欧美高清一区 | 国产亚洲综合精品 | 看一级毛片视频 | 国产成人午夜 | 中文字幕在线播放第一页 | 欧美久久精品一级c片 | 国产精品一区二 | 污网站在线免费 | 色悠悠久久 | 国产成人精品一区二区三区四区 | 99精品电影| 日韩欧美一级精品久久 | 四色成人av永久网址 | 图片区 国产 欧美 另类 在线 | 亚州精品成人 | 五月天最新网址 | 天堂一区二区三区四区 | 久久久久久久中文 | 中文字幕一区二区不卡 | 欧美一级片免费观看 | 国产成人a亚洲精品 | 国产精品视频免费 | av在线一区二区 | 亚洲一区二区三区免费在线观看 | 成人一区视频 | 欧美成人精品一区二区男人看 | 日韩欧美在线观看视频网站 | 精品亚洲一区二区三区 | 欧美性一区二区 | 精品中文字幕在线观看 | 亚洲国产精品av | 欧美亚洲高清 | 九九免费视频 |