| |
常用链接
留言簿
我参与的团队
我的标签
随笔档案
最新随笔
搜索
最新评论

阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
1 新建“空白解决方案”,添加WEB SETUP项目
2 添加website空白文件夹夹内的文件。
App_Data\Material_DB_Data.MDF
App_Data\Material_DB_Log.LDF 用来附加的数据库
Snap1.ico 快捷方式图标文件
Web.Config (注:事先在文件里添加了connectionstring
<add name="dbconnectionString" connectionString="server=(local);uid=sa;pwd=;database=material_db"/>
)
剩下的都是web网页文件了。
全部拖入web站点文件夹,之后右键属性=》安装包 选中系统依赖=》MDAC 2.8 和 .NET 2.0 FRAMWROK
3 切换安装视图=》用户界面=》添加一个Textbox(A) 4个文本框 ,修改其属性
BannerBitmap (None)
BannerText 安装数据库
BodyText 安装程序将在目标机器上安装数据库
Edit1Label 数据库名称:
Edit1Property DBNAME
Edit1Value Material_DB
Edit1Visible True
Edit2Label 服务器名:
Edit2Property SERVER
Edit2Value (local)
Edit2Visible True
Edit3Label 用户名:
Edit3Property USER
Edit3Value sa
Edit3Visible True
Edit4Label 密码:
Edit4Property PWD
Edit4Value
Edit4Visible True
同样可以修改WEB安装计划的属性
Misc
AddRemoveProgramsIcon (None)
Author google想要统治丢球
Description 不需要描述
DetectNewerInstalledVersion False
Keywords
Localization Chinese (Simplified)
Manufacturer 微软
ManufacturerUrl www.microsoft.com
PostBuildEvent
PreBuildEvent
ProductCode {153A5F79-1E50-4ECD-B469-090A32EC2726}
ProductName google想要统治地球
RemovePreviousVersions False
RestartWWWService True
RunPostBuildEventOn successful build
SearchPath
Subject
SupportPhone
SupportUrl
TargetPlatform x86
Title google想要统治地球
UpgradeCode {39DEFB52-8F2F-47D1-9AB7-1D21691A8DF4}
Version 1.0.0
具体请看视频 (注意不要遗漏将 resourceList.xml 设置为“嵌入的资源”,为Custome Action 中的install 定义DLL输出 设置DBNAME等参数,为class library 添加 Interop.IWshRuntimeLibrary DLL 本篇使用该DLL注册快捷方式 )
Visual Studio 2005 how to make a install package from gakaki on Vimeo
Untitled from gakaki on Vimeo
resourceList.xml 文件
为了项目修改方便使用XML 注意数据库我将其放置在App_Data中了,ico文件在根目录下,
<?xml version="1.0" encoding="utf-8" ?>
<configroot>
<Files>
<MDF>
<File name="Material_DB_Data.MDF"/>
</MDF>
<LDF>
<File name="Material_DB_Log.LDF"/>
</LDF>
<图标ICO名>
<File name="Snap1.ico"/>
</图标ICO名>
<默认首页>
<File name="Default.aspx"/>
</默认首页>
</Files>
</configroot>
ShortCut.cs 生成桌面或者开始菜单快捷方式
 Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using IWshRuntimeLibrary;

namespace InstallHelper
  {
class ShortCut
 {

public static void 生成快捷方式(string 存放路径地址, string 快捷方式名字, string 链接到哪里, string 描述, string ico文件路径, string 若存在提示消息, ref string 保存的路径)
 {
string 添加到如下地址 = 保存的路径 = Path.Combine(存放路径地址, 快捷方式名字 + ".lnk");
WshShell wshShell = new WshShellClass();//创建 Windows Script Host Shell 类
IWshShortcut favShortcut = (IWshShortcut)wshShell.CreateShortcut(添加到如下地址);//定义快捷方式文件
favShortcut.TargetPath = 链接到哪里;
favShortcut.WorkingDirectory = System.Environment.CurrentDirectory;
favShortcut.WindowStyle = 1;
favShortcut.Description = 描述;
favShortcut.IconLocation = string.Format("{0},0", ico文件路径);

if (System.IO.File.Exists(添加到如下地址))
 {
if (
MessageBox.Show(string.Format("{0}快捷方式已经存在,是否覆盖?", 若存在提示消息), string.Format("安装{0}快捷方式", 若存在提示消息), MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1) == DialogResult.Yes
)
 {

favShortcut.Save();//保存快捷方式
}
}
else
 {
favShortcut.Save();
}

}

}
}

加密web.config文件 (还不完美 ,加密的时候会弹出DOS窗口,bat文件需要转码,不然不支持中文路径)
 Code
namespace InstallHelper
  {
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
class EncryptWebConfig
 {
 /**//// <summary>
/// 调用写一个加密的BAT文件 之后调用那个bat文件 加密 web.config Encrypts the web config.
/// </summary>
/// <param name="targetdir">The targetdir.</param>
public static void EncryptTheWebConfig(string targetdir)
 {

WriteABatchToEncryptBat(targetdir);
string batFileName = Path.Combine(targetdir, "encrypt.bat") ; //这个方法会拼接 路径和文件 => 路径/xx.bat
//MessageBox.Show(batFileName);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//隐藏式执行
p.StartInfo = new System.Diagnostics.ProcessStartInfo(batFileName);
p.Start();
p.WaitForExit(); //等待执行完毕


//执行完删除.bat
 if (File.Exists(batFileName)) { File.Delete(batFileName); }


}

 /**//// <summary>
///撰写一个加密web.config的文件 Writes the A batch to encrypt bat.
/// </summary>
/// <param name="targetdir">The targetdir.</param>
public static void WriteABatchToEncryptBat(string targetdir)
 {
string dir = Path.Combine(targetdir, "encrypt.bat");
StreamWriter EncryptBatFileStream = System.IO.File.CreateText(dir);
EncryptBatFileStream.WriteLine("echo off");
EncryptBatFileStream.WriteLine(@"PATH %PATH%;%SystemRoot%\Microsoft.NET\Framework\v2.0.50727");
 string encrypt = string.Format("aspnet_regiis -pef connectionStrings \" {0}\"", targetdir);
EncryptBatFileStream.WriteLine(encrypt);
EncryptBatFileStream.Flush();
EncryptBatFileStream.Close();
EncryptBatFileStream.Dispose();

UTF82ANSI(dir);

}

private static void UTF82ANSI(string filepath)
 {
//bat文件为ansi格式 需要转换
StreamReader sr = new StreamReader(filepath, Encoding.UTF8, false);
string data = sr.ReadToEnd();
sr.Close();
StreamWriter sw = new StreamWriter(filepath, false, Encoding.Default);
sw.Write(data);
sw.Close();
}


 /**//*
关于如何加密配置信息:
http://blog.joycode.com/ghj/archive/2006/02/12/71378.aspx

http://msdn2.microsoft.com/en-us/library/zhhddkxy.aspx
http://blogs.msdn.com/federaldev/archive/2005/11/08/490319.aspx
http://msdn2.microsoft.com/en-us/library/dtkwfdky.aspx
http://aspdot.net/articles/encryptedconnstring/
http://davidhayden.com/blog/dave/archive/2005/11/17/2572.aspx
http://weblogs.asp.net/owscott/archive/2005/07/29/421063.aspx
加密批处理:
echo off
PATH %PATH%;%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
aspnet_regiis -pef "connectionStrings" ShopWeb -prov "RsaProtectedConfigurationProvider"
aspnet_regiis -pef "connectionStrings" ManagerWeb -prov "RsaProtectedConfigurationProvider"
@PAUSE

解密批处理:
echo off
PATH %PATH%;%SystemRoot%\\Microsoft.NET\Framework\v2.0.50727
aspnet_regiis -pdf "connectionStrings" ShopWeb
aspnet_regiis -pdf "connectionStrings" ManagerWeb
@PAUSE

ShopWeb和ShopWeb是当前目录下的一个子目录。

*/
}
}

安装类 InstallerDB.cs
 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using InstallHelper;
using System.Windows.Forms;
using System.Diagnostics;


namespace InstallDB
  {
[RunInstaller(true)]
public partial class InstallerDB : Installer
 {
public InstallerDB()
 {
InitializeComponent();
}


public override void Install(System.Collections.IDictionary stateSaver)
 {

base.Install(stateSaver);
try
 {

 各配置文件夹 变量初始化#region 各配置文件夹 变量初始化

//读取指定xml文件中的配置信息 数据库,图标ICO名字 方便将来修改 只要替换xml文件就可以了
System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("InstallDB.resourceList.xml");
XmlDocument config = new XmlDocument();
config.Load(stream);

string TargetstrMdfName = "";
string TargetstrLdfName = "";
string iconName = "";
string 默认首页 = "";

if (config != null)
 {
TargetstrMdfName = config.SelectSingleNode("configroot/Files/MDF/File").Attributes["name"].Value;
TargetstrLdfName = config.SelectSingleNode("configroot/Files/LDF/File").Attributes["name"].Value;
iconName = config.SelectSingleNode("configroot/Files/图标ICO名/File").Attributes["name"].Value;
默认首页 = config.SelectSingleNode("configroot/Files/默认首页/File").Attributes["name"].Value;
}
else
 {
throw new InstallException("没有找到指定数据库文件位置的xml!");
}

string web安装文件夹 = Context.Parameters["targetdir"].ToString();//注意 该路径 末尾多一个"\" 在 安装程序中默认设置了targetdir H:\Inetpub\wwwroot\wss\VirtualDirectories\80\发卡清算系统324234\

string web安装文件夹末尾没有斜杠 = web安装文件夹.Remove(web安装文件夹.LastIndexOf("\\"),1);
string webDirWeb虚拟文件夹 = GetWebDir(web安装文件夹);

string MDF文件路径 = web安装文件夹 + "App_Data\\" + TargetstrMdfName;
string LDF文件路径 = web安装文件夹 + "App_Data\\" + TargetstrLdfName;
string ICON图标路径 = Path.Combine(web安装文件夹, iconName);
string 数据库附加名字 = this.Context.Parameters["dbname"].ToString();
#endregion

//MessageBox.Show(web安装文件夹末尾没有斜杠);
//MessageBox.Show(web安装文件夹);
//MessageBox.Show(webDirWeb虚拟文件夹);

//MessageBox.Show(MDF文件路径);
//MessageBox.Show(LDF文件路径);
//MessageBox.Show(ICON图标路径);

//获取附加SQL 数据库 语句命令
string AttachDbSQLStr = GetAttachDbSQLStr("dbname", MDF文件路径, LDF文件路径);
//MessageBox.Show(AttachDbSQLStr);


//执行SQL 附加数据库
ExecuteSql(GetSqlConnectionStrDBMaster("server", "user", "pwd"), AttachDbSQLStr);

//使用安装包中指定参数替换web.config字符串 注意这里的dbconnectionString是web.config中本来我指定的
WriteWebConfig(GetSqlConnectionStr("server", "user", "pwd", "dbname"), "targetdir", "dbconnectionString");

//加密web.config
InstallHelper.EncryptWebConfig.EncryptTheWebConfig(web安装文件夹末尾没有斜杠);

//快捷方式
string 开始菜单快捷方式_保存路径 = "";
string 桌面快捷方式_保存路径="";


开始和桌面快捷方式保存(ICON图标路径, ref 开始菜单快捷方式_保存路径, ref 桌面快捷方式_保存路径, webDirWeb虚拟文件夹, 默认首页);


}
catch (Exception ex)
 {
MessageBox.Show(ex.ToString());
throw ex;
}

&n | |