现在的位置: 首页 > 综合 > 正文

winform打包安装

2014年04月17日 ⁄ 综合 ⁄ 共 6981字 ⁄ 字号 评论关闭

创建自定义安装包

在写好的软件解决方案下新添加一个安装与部署项目,添加主程序的输出等这些和平常打包一样。然后在安装与部署项目上右键,选择视图中的[用户界面],添加入选择的自定义对话框,比如[文本框(A)].然后设置这个新对话框的属性,看用几个文本框,并都设上显示的文本及editproperty属性。这里就是给文本框定义个名字,以后可这个名字取到文本框的值。这里我设的就用了一个文本框 editproperty是INDEX。
然后要写自定义操作的方法了,还是在原软件解决方案下新添加一个安装类库,添加System.Configuration.Install的引用。在类里继承: System.Configuration.Install.Installer,然后重写Install方法
public override void Install(System.Collections.IDictionary stateSaver)
   {
   
     System.IO.StreamWriter sw = new StreamWriter(@"d:/setup.txt",true);
     sw.WriteLine(Context.Parameters["key"].Trim() + Context.Parameters["targetDir"].Trim());
     sw.Flush();
     sw.Close();

}
这样自定义的操作就写完了。上面的方法就是取传的参数值写到文件里。
这个类的全部代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Windows.Forms;
namespace DBCustomAction
{
/// <summary>
/// DBCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;
   public DBCustomAction() :base()
   {
  
    InitializeComponent();
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if(components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region 组件设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    components = new System.ComponentModel.Container();
   }
   #endregion
  
   # region 创建数据库
   public void ExecuteSql(string conn,string databaseName,string sql)
   {
    SqlConnection con = new SqlConnection(conn);
    SqlCommand com = new SqlCommand(sql,con);
    con.Open();
    com.Connection.ChangeDatabase(databaseName);
    try
    {
     com.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
     ex.ToString();
    }
    finally
    {
     con.Close();
    }
   }
   #endregion

   public override void Install(System.Collections.IDictionary stateSaver)
   {
    try
    {
     System.IO.StreamWriter sw = new StreamWriter(@"d:/setup.txt",true);
     sw.WriteLine(Context.Parameters["key"].Trim() + Context.Parameters["targetDir"].Trim());
     sw.Flush();
     sw.Close();
    
//     //连接字符串
//     string conStr = String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096",this.Context.Parameters["server"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"]);
//     StreamWriter sw = new StreamWriter(@"c:/rizhi.txt",true);
//     sw.WriteLine(conStr + "   "+ DateTime.Now.ToShortDateString()+DateTime.Now.ToLongTimeString());
//     sw.Close();
//     //执行创建数据库
//     ExecuteSql(conStr,"master","create database " + this.Context.Parameters["dbname"]);
//     //用osql.exe执行脚本文件
//     StreamWriter ssw = new StreamWriter(@"c:/rizhi.txt",true);
//     ssw.WriteLine(" Create Sql Ok "+ DateTime.Now.ToShortDateString()+DateTime.Now.ToLongTimeString());
//     ssw.Close();
//     System.Diagnostics.Process proc = new System.Diagnostics.Process();
//     proc.StartInfo.FileName ="osql.exe";
//     proc.StartInfo.Arguments = String.Format(@" -U {0} -P {1} -d {2} -i {3}createWeb.sql",Context.Parameters["uid"], Context.Parameters["pwd"], Context.Parameters["dbname"], Context.Parameters["targetdir"]);
//     proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//     proc.Start();
//     proc.WaitForExit();
//     proc.Close();
//     StreamWriter sssw = new StreamWriter(@"c:/rizhi.txt",true);
//     sssw.WriteLine(" Osql Oper OK "+ DateTime.Now.ToLongTimeString());
//     sssw.Close();
//     //删除脚本文件
//     if(File.Exists(String.Format("{0}createWeb.sql",this.Context.Parameters["targetdir"])))
//     {
//      File.Delete(String.Format("{0}createWeb.sql",this.Context.Parameters["targetdir"]));
//      StreamWriter ssssw = new StreamWriter(@"c:/rizhi.txt",true);
//      ssssw.WriteLine(" Delete Oper OK "+ DateTime.Now.ToShortDateString()+DateTime.Now.ToLongTimeString());
//      ssssw.Close();
//     }
//     string webConfigPath = this.Context.Parameters["targetdir"]+"Web.config";
//     //更改web.config
//     XmlDocument config = new XmlDocument();
//     config.Load(webConfigPath);
//     foreach(XmlNode node in config.DocumentElement.ChildNodes[0].ChildNodes)
//     {
//      if(node.Name =="add")
//      {
//       //       if(node.Attributes.GetNamedItem("key").Value == "ModServer")
//       //       {
//       //        node.Attributes.GetNamedItem("value").Value = String.Format( String.Format("data source={0};user id={1};password={2};database={3};persist security info=false;packet size=4096;Pooling=true;Max Pool size=100;Min pool size=1",this.Context.Parameters["server"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"],Context.Parameters["dbname"]));
//       //      
//       //       }
//       if(node.Attributes.GetNamedItem("key").Value == "CreateUserdatabase")
//       {
//        node.Attributes.GetNamedItem("value").Value = String.Format( String.Format("data source={0};user id={1};password={2};database=master;persist security info=false;packet size=4096;",this.Context.Parameters["server"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"]));
//       }
//       if(node.Attributes.GetNamedItem("key").Value == "createServer")
//       {
//        node.Attributes.GetNamedItem("value").Value = String.Format( String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096;Pooling=true;Max Pool size=100;Min pool size=10;database=",this.Context.Parameters["server"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"],Context.Parameters["dbname"]));
//        StreamWriter change = new StreamWriter(@"c:/rizhi.txt",true);
//        change.WriteLine(" Edit Oper OK "+ DateTime.Now.ToShortDateString()+DateTime.Now.ToLongTimeString());
//        change.Close();
//      
//       }
//       if(node.Attributes.GetNamedItem("key").Value == "SysServer")
//       {
//        node.Attributes.GetNamedItem("value").Value = String.Format( String.Format("data source={0};user id={1};password={2};database={3};persist security info=false;packet size=4096;Pooling=true;Max Pool size=100;Min pool size=1",this.Context.Parameters["server"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"],Context.Parameters["dbname"]));
//     
//       }
//      }
//     }
//
//     config.Save(webConfigPath);

    }
    catch(Exception ex)
    {
     StreamWriter ew = new StreamWriter(@"c:/error.txt",true);
     ew.WriteLine(ex.ToString()+"/r/n"+DateTime.Now.ToShortDateString()+DateTime.Now.ToLongTimeString());
     ew.Close();
    }
    finally
    {

    }

   }
}
}

然后在安装项目里再添加这个自定义操作项目的主输出。在视图里选择[自定义操作],在[安装]下把自定义操作项目的主输出添加进来,右键 属性里 把customActionData 设上值,这里是/key="[INDEX]" /targetDir="[TARGETDIR]/"。
这样就可以在安装时输入文本框的信息再进行自定义操作了。

 

 

 

=========================================================================

如果你希望在安装完后,程序运行前先做一些配置的工作(比如说对数据库进行配置),本文将告诉你如何制作一个这样的安装包

1.新建一个项目,实现一个配置窗口(Form1)

2.在该项目中添加一个自定义的安装类,Installer的派生类

3.Override函数Install

4.编译生成可执行文件(myinstall.exe)

5.新建安装项目,把myinstall.exe添加进来

6.打开自定义操作(视图/自定义操作),添加自定义操作,选择myinstall.exe

7.设置自定义操作的属性CustomActionData=/configFile="myinstall.exe"/targetDir="[TARGETDIR]/"

8.生成安装包,并测试运行安装包,在安装完成后就回弹出配置窗口Form1

抱歉!评论已关闭.