最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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>
        /// 測試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("測試通過", "系統(tǒng)提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("網(wǎng)站地址有誤", "系統(tǒng)提示", 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("安裝路徑有誤", "系統(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("在安裝和啟動服務(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)文章

  • Visual C#中如何使用IComparable和IComparer接口

    Visual C#中如何使用IComparable和IComparer接口

    這篇文章主要介紹了C#中使用IComparable和IComparer接口,在本例中,該對象被用作第二個(gè)參數(shù)被傳遞給Array.Sort的接受IComparer實(shí)例的重載方法,需要的朋友可以參考下
    2023-04-04
  • C#判斷系統(tǒng)是32位還是64位的方法

    C#判斷系統(tǒng)是32位還是64位的方法

    這篇文章主要介紹了C#判斷系統(tǒng)是32位還是64位的方法,實(shí)例分析了兩種常用的技巧供大家選擇使用,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#使用log4net記錄日志

    C#使用log4net記錄日志

    本文詳細(xì)講解了C#使用log4net記錄日志的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • C#實(shí)現(xiàn)拆分字符串的示例詳解

    C#實(shí)現(xiàn)拆分字符串的示例詳解

    這篇文章主要為大家詳細(xì)介紹了C#如何分別使用正則表達(dá)式Regex.Split方法和String.Split方法實(shí)現(xiàn)拆分字符串,有需要的小伙伴可以參考一下
    2024-02-02
  • C#復(fù)合模式(Composite Pattern)實(shí)例教程

    C#復(fù)合模式(Composite Pattern)實(shí)例教程

    這篇文章主要介紹了C#復(fù)合模式(Composite Pattern),以實(shí)例形式講述了復(fù)合模式在樹形結(jié)構(gòu)中的應(yīng)用,需要的朋友可以參考下
    2014-09-09
  • c# socket心跳超時(shí)檢測的思路(適用于超大量TCP連接情況下)

    c# socket心跳超時(shí)檢測的思路(適用于超大量TCP連接情況下)

    這篇文章主要介紹了c# socket心跳超時(shí)檢測的思路(適用于超大量TCP連接情況下),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • 詳解c# 類型轉(zhuǎn)換

    詳解c# 類型轉(zhuǎn)換

    這篇文章主要介紹了c# 類型轉(zhuǎn)換的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以學(xué)習(xí)
    2020-07-07
  • C#基于COM方式讀取Excel表格的方法

    C#基于COM方式讀取Excel表格的方法

    這篇文章主要介紹了C#基于COM方式讀取Excel表格的方法,涉及C# COM組件的調(diào)用與Excel表格的使用技巧,需要的朋友可以參考下
    2016-07-07
  • C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法

    C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法

    這篇文章主要介紹了C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法,即windows forms編程中取消最小化、最大化、關(guān)閉按鈕,需要的朋友可以參考下
    2014-08-08
  • C#實(shí)現(xiàn)工廠方法模式

    C#實(shí)現(xiàn)工廠方法模式

    這篇文章介紹了C#實(shí)現(xiàn)工廠模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評論

象州县| 靖远县| 嘉鱼县| 三明市| 佛冈县| 牟定县| 海南省| 上高县| 金坛市| 庆安县| 洪洞县| 宣武区| 隆昌县| 周宁县| 凉山| 永顺县| 施秉县| 荣昌县| 宁德市| 柘城县| 安远县| 奇台县| 长泰县| 夏河县| 固安县| 石门县| 广丰县| 吴桥县| 虞城县| 延安市| 永兴县| 雷山县| 任丘市| 普陀区| 天门市| 思南县| 方城县| 富川| 普兰店市| 芦山县| 德格县|