做了一个web的打包程序,和大家分享一下。

第一步:新建——文件——项目,弹出对话框

如图,选择安装和部署——安装项目
这里要解释一下了,一般来说,制作web安装程序选择web安装项目,而我没有选择web安装项目的原因是web安装项目制作出来的安装包是不可以选择软件的安装路径的,而安装项目制作出来的安装包是可以选择软件的安装目录的。
第二步:右键解决方案中的安装项目——视图——文件系统
 如图
弹出

这样的文件系统界面,将你发布完的文件复制到应用程序文件夹下
第三步:右键解决方案中的安装项目——视图——用户界面(和第二步一样,不截图了)弹出下图
其中客户信息根据项目需求可有可无,文本框(A)文本框(B)要是配置数据库连接和虚拟路径的朋友们还是要加的,移动到安装文件夹之上。
第四步:右键文本框(A)——属性窗口,如图
 
解释一下,Edit1Property和Edit2Property是自定义的,朋友们可以随便写,但是要有意义并且能记住,等会儿会用的。
第五步:右键文本框(B)——属性窗口,如图
 在解释,Edit1Property,Edit2Property,Edit3Property,Edit4Property均同上,唯一解释的是Edit2Value中填写的值是默认值,朋友们根据个人情况选填。
第六步:右键解决方案——添加——新建项目——类库,删除Class1.CS文件。在这个类库上右键——添加——新建项
如图,选择安装程序类
第七步:右键解决方案中的安装项目——添加——项目输出,如下图
下拉框中就是你刚才建的类库名称,选择主输出——确定。
第八步:右键解决方案中的安装项目——视图——自定义操作,如下图
右键安装——添加自定义操作,选择应用程序文件夹下面的主输出——确定。
这时在安装文件夹下面就多了一个主输出,点击一下,属性如下图
图片可能看不清楚,主要只改一个属性,CustomActionData
这个值很重要
/dbname=[DBNAME] /password=[PASSWORD] /user=[USERNAME]  /server=[DBSERVERNAME] /iis=[IISSERVER] /port=[PORT] /targetdir="[TARGETDIR]/"
这么重要就解释一下,小写的字母全是你建的那个安装程序类中要用到的字段,这个属性主要就是传值用的,将安装信息通过这个属性传入给安装程序类,大写的字母就是当时文本框(A)文本框(B)中你自定义的那些值,targetdir是获得软件的安装目录用的。
到这里,基本上就没有安装项目什么事了,接下来重点在安装程序类。

首先:准备工作,我所用到的引用
以下是这个类的所有代码
using System;
using System.IO;
using System.DirectoryServices;
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;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using IWshRuntimeLibrary;
using System.Windows.Forms;
using System.Diagnostics;

namespace ***ClassLibrary
{
    [RunInstaller(true)]
    public partial class ***Installer : Installer
    {
        public ***Installer()
        {
            InitializeComponent();
        }
        #region 变量
        private string server = string.Empty;
        private string dbname = string.Empty;
        private string user = string.Empty;
        private string password = string.Empty;
        private string dir = string.Empty;
        private string iis = string.Empty;
        private string port = string.Empty;
        #endregion

        #region 创建快捷方式(这个WEB快捷方式的创建方法,就是在桌面上创建了一个url的文件)
        private void CreateKJFS()
        {
            StreamWriter sw = new StreamWriter(System.IO.File.Open(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "", FileMode.Create, FileAccess.Write));
            sw.WriteLine("[InternetShortcut]");
            sw.WriteLine("URL=http://"+iis+"/" + port);
            sw.WriteLine("IconFile=" + dir.Substring(0, dir.LastIndexOf("//")) + "");
            sw.WriteLine("IconIndex=0");
            sw.Flush();
            sw.Close();
        }
        #endregion

        #region 移动数据库(解释,为什么要移动数据库呢?主要是我的数据库文件是和发布文件一起放入了应用程序文件夹中,这样在卸载的时候就会默认卸载掉我的数据库,后果十分严重,所以,我在安装目录下创建了一个文件夹Data,根据条件移动文件,由于是代码自动创建的文件夹,所以卸载时就不会被卸载掉了)
        private void MoveData()
        {
            if (!Directory.Exists(dir + "")) //如果不存在Data文件夹
            {
                Directory.CreateDirectory(dir + ""); //创建Data文件夹
                Directory.Move(dir + "", dir + ""); //移动文件
                Directory.Move(dir + "", dir + ""); //同上
                AddDB();//附加数据库(方法在下面)
            }
            else //如果存在Data文件夹
            {
                if (System.IO.File.Exists(dir + "") && System.IO.File.Exists(dir + "")) //并且存在数据库文件
                {
                    LeaveDB(); //分离数据库
                    DialogResult r1 = MessageBox.Show("您确定要用原始数据替换您的现有数据吗?替换前建议您先备份好现有数据,一旦替换,数据将无法恢复!","注意", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
                    int ss1 = (int)r1;
                    if (ss1 == 6) //如果确定替换
                    {
                        System.IO.File.Delete(dir + ""); //删除文件
                        System.IO.File.Delete(dir + ""); //同上
                        Directory.Move(dir + "", dir + ""); //移动文件
                        Directory.Move(dir + "", dir + ""); //同上
                        AddDB(); //附加数据库
                    }
                    else
                    {
                        AddDB(); //附加数据库
                    }
                }
                else //存在文件夹但是不存在文件
                {
                    Directory.Move(dir + "", dir + "");
                    Directory.Move(dir + "", dir + "");
                    AddDB();
                }
            }
        }
        #endregion

        #region 附加数据库
        private void AddDB()
        {
            string connStr = string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", server, user, password);
            string strSql = "if not exists (select * From master.dbo.sysdatabases where name='" + dbname + "') begin EXEC sp_attach_db  @dbname  =  N'" + dbname + "'," + "@filename1  =  N'" + dir + "," + "@filename2  =  N'" + dir + " end";
            ExecuteSql(connStr, "master", strSql);
        }
        #endregion

        #region 分离数据库
        private void LeaveDB()
        {
            string connStr = string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", server, user, password);
            string strSql = "EXEC sp_detach_db '数据库名称', 'true' ";
            ExecuteSql(connStr, "master", strSql);
        }
        #endregion

        #region 创建虚拟目录(这段是网上copy的,不是自己做的不敢解释)
        private void CreateVirtualDir()
        {
            try
            {
                string constIISWebSiteRoot = "IIS://" + iis + "/W3SVC/1/ROOT";
                DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
                DirectoryEntry newRoot = root.Children.Add(port, root.SchemaClassName);
                newRoot.Properties["Path"][0] = dir;
                newRoot.Properties["AppIsolated"][0] = 2;             // 值 0 表示应用程序在进程内运行,值 1 表示进程外,值 2 表示进程池
                newRoot.Properties["AccessScript"][0] = true;          // 可执行脚本
                newRoot.Invoke("AppCreate", true);
                newRoot.Properties["DefaultDoc"][0] = "Default.aspx";//设置起始页
                newRoot.Properties["AppFriendlyName"][0] = port;   // 应用程序名
                newRoot.CommitChanges();
                root.CommitChanges();
                string fileName = Environment.GetEnvironmentVariable("windir") + @"/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis.exe";
                ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
                //处理目录路径
                string path = newRoot.Path.ToUpper();
                int index = path.IndexOf("W3SVC");
                path = path.Remove(0, index);
                //启动aspnet_iis.exe程序,刷新教本映射
                startInfo.Arguments = "-s " + path;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                string errors = process.StandardError.ReadToEnd();
                if (errors != string.Empty)
                    throw new Exception(errors);
            }
            catch (Exception ee)
            {
                MessageBox.Show("虚拟目录创建失败!您可以手动创建! " + ee.Message, "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
            }
        }
        #endregion

        #region 修改web.config的连接数据库的字符串
        private void WriteWebConfig()
        {
            System.IO.FileInfo FileInfo = new System.IO.FileInfo(dir + "/web.config");
            if (!FileInfo.Exists) //不存在web.config文件
            {
                throw new InstallException("没有找到web.config配置文件!");
            }
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(FileInfo.FullName);
            bool FoundIt = false;
            foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
            {
                if (Node.Name == "add")
                {
                    if (Node.Attributes.GetNamedItem("name").Value == "配置文件中name的值")
                    {
                        Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Persist Security Info=False;Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", server, dbname, user, password);
                    FoundIt = true;
                    }  
                }
            }
            if (!FoundIt)
            {
                throw new InstallException("修改web.config配置文件失败!");
            }
            xmlDocument.Save(FileInfo.FullName);
        }
        #endregion

        #region 执行SQL语句所用方法
        private void ExecuteSql(string connStr, string DatabaseName, string Sql)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(Sql, conn);
            conn.Open();
            conn.ChangeDatabase(DatabaseName);
            try
            {
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

 #region Install 安装(安装主方法)
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            dir = this.Context.Parameters["targetdir"].ToString();
            server = this.Context.Parameters["server"].ToString();
            dbname = this.Context.Parameters["dbname"].ToString();
            user = this.Context.Parameters["user"].ToString();
            password = this.Context.Parameters["password"].ToString();
            iis = this.Context.Parameters["iis"].ToString();
            port = this.Context.Parameters["port"].ToString();
            //移动数据库
            MoveData();
            //创建虚拟目录
            CreateVirtualDir();
            //重写Config
            WriteWebConfig();
            //创建快捷方式
            CreateKJFS();
        }
        #endregion
    }
}

转载于:https://www.cnblogs.com/wujy/archive/2012/01/31/2333053.html

WEB程序打包详解:(连接SQL2005数据库,修改配置文件,建立虚拟目录)相关推荐

  1. 10年后端开发程序员详解数据库缓存方案到底有多少名堂。丨Linux服务器开发丨后端开发丨中间件丨web服务器丨数据库缓存

    数据库缓存方案到底有多少花样,一节课带你缕清 1. 读写分离方案 2. 若干个缓存解决方案 3. 缓存故障如何解决 视频讲解如下,点击观看: 10年后端开发程序员详解数据库缓存方案到底有多少名堂.丨L ...

  2. iPhone和Android的WEB应用开发详解

    iPhone和Android的WEB应用开发详解 在我们现在的生活中,移动设备的作用日益重要.我们使用它们进行交流.我们使用它们进行导航.我们甚至可以将它们用作方便的手电筒.面向 iPhone 和 A ...

  3. java web工程web.xml配置详解

    转载自:http://blog.csdn.net/believejava/article/details/43229361 这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢 ...

  4. javaweb:web.xml配置详解

    Web.xml详解: 1.web.xml加载过程(步骤) 首先简单讲一下,web.xml的加载过程.当启动一个WEB项目时,容器包括(JBoss.Tomcat等)首先会读取项目web.xml配置文件里 ...

  5. 详解 Visual C# 数据库编程

    详解 Visual C# 数据库编程 ****** 2007-11-05 14:34 关于数据库编程,微软提供了一个统一的数据对象访问模型,在Visual Studio6.0中称为ADO,在.NET中 ...

  6. 详解:MySQL数据库的权限管理和运维实操

    详解:MySQL数据库的权限管理 一.MYSQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行updat ...

  7. JavaWeb web.xml配置详解

    参考: XML 教程 Java web之web.xml配置详解 Javaweb三大组件是:Servlet,Filter,Listener. 1.Servlet Servlet作为中转处理的容器,连接了 ...

  8. anychat java_AnyChat的视频会议程序实例详解

    AnyChat(全名叫Anychat SDK),也叫音视频互动开发平台:是一套跨平台的即时通讯解决方案,基于先进的H.264视频编码标准.AAC音频编码标准与P2P技术,整合了佰锐科技在音视频编码.多 ...

  9. 微信小程序python flask_Python Flask 搭建微信小程序后台详解

    前言: 近期需要开发一个打分的微信小程序,涉及到与后台服务器的数据交互,因为业务逻辑相对简单,故选择Python的轻量化web框架Flask来搭建后台程序.因为是初次接触小程序,经过一番摸索和尝试,个 ...

最新文章

  1. Linux内核子系统
  2. DevExpress v19.1新版亮点——WinForms篇(五)
  3. python将文件夹打包
  4. 关于手机横屏打开相机或者相册闪退解决方案
  5. 大数据-----软件开发模型(详细讲解)
  6. 网络安全防护部署,升级支持IPv6
  7. 贪心——区间选点问题(用最少数量的箭引爆气球 Leetcode 452)
  8. mysql用户变量递归_MYSQL递归树查询的实现
  9. php htm specialchars_decode,PHP htmlspecialchars和htmlspecialchars_decode(函数)
  10. 推荐系统用户行为分析
  11. python状态码及其含义_Shell退出状态码及其应用详解
  12. 迁移到 Centos 7 遇到的一些常见问题
  13. 图神经网络 图像处理,神经网络图像修复
  14. IntelliJ IDEA 激活 破解补丁
  15. java 获取excel的行数_JAVA使用POI获取Excel的列数与行数
  16. VC++计算正反坐标方向角
  17. Quartus (Quartus Prime 18.1)的安装及仿真步骤
  18. wince buid
  19. Python入门到实践(上)(牛客网题库)day2
  20. Win7下基于Anaconda安装TensorFlow

热门文章

  1. ARM指令寻址方式之: 数据处理指令的寻址方式
  2. Android 官网无法访问的解决方法
  3. h5封装去底部_干货分享 | 一步一步教你在SpringBoot中集成微信支付H5支付
  4. 微信小程序 事件点击后如何动态增删class类名(自用,没毛病)
  5. 自认为有必要学习的Sql 总结,积累 mybatis
  6. 每天一道LeetCode-----数组序列,每个元素的值表示最多可以向后跳多远,计算最少跳多少次可以到达末尾
  7. 学习16位DOS汇编笔记
  8. 犯了一个连接数据库的低级错误
  9. uC/OS II--与ECB操作相关的四个函数
  10. 第十一届河南省赛--A计划日