当前位置:  编程技术>.net/c#/asp.net

c#制作类似qq安装程序一样的单文件程序安装包

    来源: 互联网  发布时间:2014-10-26

    本文导语:  代码如下: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;u...

代码如下:

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 dic;
        Thread t;
        public MainForm()
        {

            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dic = new Dictionary();
            groupBox1.Visible = true;
            groupBox2.Visible = false;
        }

        ///
        /// 安装路径
        ///
        ///
        ///
        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);
                    }
                }
            }
        }
        ///
        /// 日志路径
        ///
        ///
        ///
        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);
                }
            }
        }
        ///
        /// 测试webservice;
        ///
        ///
        ///
        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;
        }
        ///
        /// 测试webservice
        ///
        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();
                }

            }
        }
        ///
        /// 开始安装
        ///
        ///
        ///
        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();
        }
        ///
        /// 安装线程
        ///
        void InstallJob()
        {
            WriteLine("准备安装环境...");
            var configPath = Path.Combine(dic["installPath"], "MON.WS.exe.config");
            var exePath = configPath.TrimEnd(".config".ToCharArray());
            var args = new List();

            args.Add(@"net stop 服务器性能监控UTRY");
            args.Add(@"%SystemRoot%Microsoft.NETFrameworkv2.0.50727InstallUtil.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.NETFrameworkv2.0.50727InstallUtil.exe " + exePath);
            args.Add(@"net start 服务器性能监控UTRY");
            args.Add("exit");
            if (!cmdCommand(args))
            {
                WriteLine("在安装和启动服务时出现异常");
                WriteLine("安装失败");
                return;
            }
            WriteLine("服务安装成功");
            WriteLine("安装成功");
        }
        ///
        /// 释放exe
        ///
        ///
        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;
            }
        }
        ///
        /// 释放Config
        ///
        ///
        ///
        bool releaseConfig(Dictionary 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;
            }
        }
        ///
        /// 执行CMD命令
        ///
        ///
        bool cmdCommand(List 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);
        ///
        /// 更新安装信息
        ///
        ///
        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;
                }
            }
        }
        ///
        /// 取消
        ///
        ///
        ///
        private void CancelBTN_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        ///
        /// 完成
        ///
        ///
        ///
        private void CompleteBTN_Click(object sender, EventArgs e)
        {
            if (t != null)
            {
                t.Abort();
                t.Join();
            }
            this.Close();
        }
    }
}

    
 
 

您可能感兴趣的文章:

  • 想安装samba服务,想制作iso镜像来挂载,如何制作iso文件?
  • 请问:哪种工具可以制作安装程序,安装程序是基于java的或是能在aix运行?
  • Java安装程序制作工具 IzPack
  • 如何利用Linux安装盘制作启动盘?
  • 请问如何制作Solaris Unix的安装软盘(用于启动安装)?
  • 跨平台安装程序制作工具 BitRock InstallBuilder
  • 安装程序制作工具 VAInstall
  • 求教,如何制作linux系统下的软件安装盘?立即给分!
  • 安装程序制作工具 InstallShield
  • 请问怎么制作安装程序
  • Java安装程序制作工具 JExpress
  • Windows安装程序制作工具 NSIS
  • linux精装得安装盘怎么制作?
  • 如何制作一个Linux启动安装盘????
  • 安装包制作工具 DebBuilder
  • 怎样制作成.exe的安装文件?
  • 安装程序制作工具 Setup Factory
  • 请问:怎样制作FreeBSD的安装盘?
  • 在linux下如何制作一个安装程序
  • 用rpm制作软件安装包
  • 教你如何用C#制作文字转换成声音程序
  • C# 透明窗体制作实现方法比较分析
  • C#制作鹰眼的详细全过程(带注释)实例代码
  • 解析C#自定义控件的制作与使用实例的详解
  • C# ToolStrip制作四边停靠浮动工具栏
  • c#制作屏幕保护程序步骤(字幕屏保)
  • C#波形图控件制作示例程序
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 请问Gnome程序如何制作?
  • 我在suse下面写了c程序软件,如何才能制作成安装版的软件了,请高手指点
  • 寻求linux命令行程序远程web界面的制作方法
  • 怎样制作一个图形界面的安装程序.
  • linux下c+程序制作安装包
  • 请问我写的程序如何制作makefile
  • 用installAnywhere制作的安装程序不能运行!
  • 请问那里有兼职的程序员可以做?(语言限于java)有网页制作的也可以呀!
  • 请问如何在Solaris下制作安装程序
  • JBuilder下如何制作类似Console程序,或者说如何设置弹出的那个对话框参数?
  • 如何制作Linux应用软件的安装程序
  • 请问1.44M的boot.img如何制作引导盘(redhat)的,如要用其他的程序,可以发到我的信箱中去吗?
  • 工作流制作的网页程序 Yeoman
  • videocapture库制作python视频高速传输程序
  • 急!急!急!急!急!本人制作的Makefile文件和程序,执行时错误,
  • 同志位我从没用过Linux到可以开发Linux程序到可以自己制作Linux需要多长时间(up有分)
  • linux下如何用C制作录音程序
  • .net+mssql制作抽奖程序思路及源码
  • 简易Dota改键外挂程序制作
  • ubuntu8.10有个制作u盘启动盘的功能,谁能制作个u盘启动盘的iso给我?小点的,有桌面的。
  • Destoon模板怎么制作?destoon模板制作简单教程
  • usb闪存启动盘制作工具 Flashboot
  • 制作usb启动盘
  • USB启动盘制作软件 iBurnMgr
  • 图标、光标制作工具 Greenfish
  • 探讨下启动盘的制作原理!
  • 如何制作FC4启动光盘(grub)?
  • 图表制作软件 Openchart2
  • ThinkPHP标签怎么制作?thinkphp标签制作实例教程
  • 电子杂志制作软件 Scribus
  • 利用Wordpress轻松制作电子书 Anthologize


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3