WEB程序打包详解:(连接SQL2005数据库,修改配置文件,建立虚拟目录)
做了一个web的打包程序,和大家分享一下。
第一步:新建——文件——项目,弹出对话框
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;
{
[RunInstaller(true)]
public partial class ***Installer : Installer
{
public ***Installer()
{
InitializeComponent();
}
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
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
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
private void AddDB()
{
ExecuteSql(connStr, "master", strSql);
}
#endregion
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
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();
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
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);
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);
}
}
if (!FoundIt)
{
throw new InstallException("修改web.config配置文件失败!");
}
xmlDocument.Save(FileInfo.FullName);
}
#endregion
private void ExecuteSql(string connStr, string DatabaseName, string Sql)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(Sql, conn);
conn.ChangeDatabase(DatabaseName);
try
{
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
#endregion
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数据库,修改配置文件,建立虚拟目录)相关推荐
- 10年后端开发程序员详解数据库缓存方案到底有多少名堂。丨Linux服务器开发丨后端开发丨中间件丨web服务器丨数据库缓存
数据库缓存方案到底有多少花样,一节课带你缕清 1. 读写分离方案 2. 若干个缓存解决方案 3. 缓存故障如何解决 视频讲解如下,点击观看: 10年后端开发程序员详解数据库缓存方案到底有多少名堂.丨L ...
- iPhone和Android的WEB应用开发详解
iPhone和Android的WEB应用开发详解 在我们现在的生活中,移动设备的作用日益重要.我们使用它们进行交流.我们使用它们进行导航.我们甚至可以将它们用作方便的手电筒.面向 iPhone 和 A ...
- java web工程web.xml配置详解
转载自:http://blog.csdn.net/believejava/article/details/43229361 这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢 ...
- javaweb:web.xml配置详解
Web.xml详解: 1.web.xml加载过程(步骤) 首先简单讲一下,web.xml的加载过程.当启动一个WEB项目时,容器包括(JBoss.Tomcat等)首先会读取项目web.xml配置文件里 ...
- 详解 Visual C# 数据库编程
详解 Visual C# 数据库编程 ****** 2007-11-05 14:34 关于数据库编程,微软提供了一个统一的数据对象访问模型,在Visual Studio6.0中称为ADO,在.NET中 ...
- 详解:MySQL数据库的权限管理和运维实操
详解:MySQL数据库的权限管理 一.MYSQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行updat ...
- JavaWeb web.xml配置详解
参考: XML 教程 Java web之web.xml配置详解 Javaweb三大组件是:Servlet,Filter,Listener. 1.Servlet Servlet作为中转处理的容器,连接了 ...
- anychat java_AnyChat的视频会议程序实例详解
AnyChat(全名叫Anychat SDK),也叫音视频互动开发平台:是一套跨平台的即时通讯解决方案,基于先进的H.264视频编码标准.AAC音频编码标准与P2P技术,整合了佰锐科技在音视频编码.多 ...
- 微信小程序python flask_Python Flask 搭建微信小程序后台详解
前言: 近期需要开发一个打分的微信小程序,涉及到与后台服务器的数据交互,因为业务逻辑相对简单,故选择Python的轻量化web框架Flask来搭建后台程序.因为是初次接触小程序,经过一番摸索和尝试,个 ...
最新文章
- Linux内核子系统
- DevExpress v19.1新版亮点——WinForms篇(五)
- python将文件夹打包
- 关于手机横屏打开相机或者相册闪退解决方案
- 大数据-----软件开发模型(详细讲解)
- 网络安全防护部署,升级支持IPv6
- 贪心——区间选点问题(用最少数量的箭引爆气球 Leetcode 452)
- mysql用户变量递归_MYSQL递归树查询的实现
- php htm specialchars_decode,PHP htmlspecialchars和htmlspecialchars_decode(函数)
- 推荐系统用户行为分析
- python状态码及其含义_Shell退出状态码及其应用详解
- 迁移到 Centos 7 遇到的一些常见问题
- 图神经网络 图像处理,神经网络图像修复
- IntelliJ IDEA 激活 破解补丁
- java 获取excel的行数_JAVA使用POI获取Excel的列数与行数
- VC++计算正反坐标方向角
- Quartus (Quartus Prime 18.1)的安装及仿真步骤
- wince buid
- Python入门到实践(上)(牛客网题库)day2
- Win7下基于Anaconda安装TensorFlow