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

좋은 웹페이지 즐겨찾기