• C#制作安装包


    软件项目编码完工后,接下来就是制作安装包了.有一些人对制作安装包不屑一顾,但我认为这是软件工程中必不可少的环节,就如何包装商品一样.我曾经经过一个星期的研究,学会了如何制作安装包.

          我做的一个安装包是可以操作配置文件的,具体步骤如下:

         1)在我已经开发好的解决方案项目中添加一个项目,选择Web安装项目,命名为你想要的名字,我这里命名为BSW_LS.

         2)在文件系统的Web应用文件夹的bin目录添加文件,文件为你的web项目里的bin目录下的dll文件,然后选中web应用文件夹,右键---->添加------>项目输出,选择内容输出.

        3)在用户界面的启动下添加文本框,用来写配置文件,我用了三个.然后对文本框的属性设置,

        4)在解决方案中添加一个新项,名称为SetupClassLibrary,在其中添加一个安装程序类,名称为MyInstall.CS,文件内容如下:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Reflection; using System.Data; using System.Data.SqlClient; using System.Configuration.Install; using System.Management; using System.Collections; using Microsoft.Win32; using System.Collections.Specialized;

    namespace SetupClassLibrary {     [RunInstaller(true)]     public partial class MyInstaller : Installer     {         //先设置私有成员,对应安装程序里接收到的用户输入         private string dbName;

            private string dbServer;

            private string dbUser;

            private string dbPwd;

            private string smtpServer;

            private string smtpUser;

            private string smtpPwd;

            private string filePath;

            private string getData;

            private string webLocation;         public MyInstaller()         {             InitializeComponent();         }         private void WriteWebConfig()         {             //加载配置文件             System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/web.config");             if (!FileInfo.Exists)             {                 throw new InstallException("Config file is lost:" + this.Context.Parameters["targetdir"] + "/web.config");             }             System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();             xmlDocument.Load(FileInfo.FullName);

                //修改连接字符串             foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])             {                 if (Node.Name == "add")                 {                     if (Node.Attributes.GetNamedItem("name").Value == "LSDBConnectionString")                     {                         Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Initial Catalog={0};Data Source={1};Persist Security Info=True;User ID={2};Password={3};", dbName, dbServer, dbUser, dbPwd);                     }

                    }             }

                foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["location"]["appSettings"])             {                 if (Node.Name == "add")                 {                     if (Node.Attributes.GetNamedItem("key").Value == "SMTPServer")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", smtpServer);                     }                     if (Node.Attributes.GetNamedItem("key").Value == "SMTPUserID")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", smtpUser);                     }                     if (Node.Attributes.GetNamedItem("key").Value == "SMTPPassword")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", smtpPwd);                     }                     if (Node.Attributes.GetNamedItem("key").Value == "path")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", filePath);                     }                     if (Node.Attributes.GetNamedItem("key").Value == "getdata")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", getData);                     }                     if (Node.Attributes.GetNamedItem("key").Value == "weblocation")                     {                         Node.Attributes.GetNamedItem("value").Value = string.Format("{0}", webLocation);                     }                 }             }             foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["applicationSettings"]["LS.Properties.Settings"]["setting"])             {                 if (Node.Name == "value")                 {                     Node.InnerText = webLocation;                 }             }             xmlDocument.Save(FileInfo.FullName);

            }

            private void ExecuteSql()         {             string connectionString = String.Format("Initial Catalog={0};Data Source={1};Persist Security Info=True;User ID={2};Password={3};", dbName, dbServer, dbUser, dbPwd);             SqlConnection conn = new SqlConnection(connectionString);             try             {                 conn.Open();             }             catch (Exception exception)             {                 throw exception;             }             finally             {                 conn.Close();             }         }

            public override void Install(IDictionary stateSaver)         {             base.Install(stateSaver);

                dbName = this.Context.Parameters["DBNAME"].ToString();

                dbServer = this.Context.Parameters["DBSERVER"].ToString();

                dbUser = this.Context.Parameters["DBUSER"].ToString();

                dbPwd = this.Context.Parameters["DBPWD"].ToString();

                smtpServer = this.Context.Parameters["SMTPSERVER"].ToString();

                smtpUser = this.Context.Parameters["SMTPUSER"].ToString();

                smtpPwd = this.Context.Parameters["SMTPPWD"].ToString();

                filePath = this.Context.Parameters["FILEPATH"].ToString();

                getData = this.Context.Parameters["GETDATA"].ToString();

                webLocation = this.Context.Parameters["WEBLOCATION"].ToString();             ExecuteSql();             //修改web.config             WriteWebConfig();

            }         public override void Uninstall(IDictionary savedState)         {             base.Uninstall(savedState);         }

        } }

    5)在文件系统界面下的Web应用程序文件夹下添加项目输出,选择主输出,选择项目SetupClassLibrary.

    6)在BSW_LS下,选择自定义操作.在安装下右键添加自定义操作,选择主输出.

    7)选中BSW_LS,生成.

    以上大致说明了一下如何制作配置参数的安装包,讲得不是很细节,因为很细节的方面网络上已经有,在些不再重复

  • 相关阅读:
    Hadoop_10_12虚拟机01_虚拟机NAT方式联网【自己的亲测笔记】
    StringUtils中 isNotEmpty 和isNotBlank的区别【java字符串判空】
    SVM
    [python]小技巧集锦
    [机器学习&数据挖掘]SVM---核函数
    [机器学习&数据挖掘]SVM---软间隔最大化
    [机器学习]SVM---硬间隔最大化数学原理
    [机器学习&数据挖掘]朴素贝叶斯数学原理
    [机器学习&数据挖掘]机器学习实战决策树plotTree函数完全解析
    [机器学习]信息&熵&信息增益
  • 原文地址:https://www.cnblogs.com/zhangsongshan/p/2352662.html
Copyright © 2020-2023  润新知