前言:最近公司有个Web要发布,但是以前都是由实施到甲方去发布,配置,这几天有点闲,同事让我搞一个一键发布,就和安装软件那样的程序,好让实施直接 配置一下数据库就可以了,然后到网上搜了下,找到一些相关的教程,现在整理了一下,花了一个下午的时间来写笔记,写好了,首先奉献给博客园的小伙伴们,和 大伙儿分享一下,好了,下面进入主题~~~ 1,首先打开VS2010,新建一个项目,如图1-1所示:
图1-1
2,然后再按照图2-1所示操作,添加现有项目,现有项目就是您要发布的Web的项目,这里只需要添加Web的那一个项目即可,类库那些可以不用添加;
图2-1
3,添加完之后,如图3-1所示
图3-1
4,接着按照图4-1所示操作,添加项目输出:
图4-1
5,如图5-1所示,添加相应的项目输出,项目选中您要发布的项目,我这里是Web,然后里面选中:"本地化资源"和"内容文件",然后确定添加.
图5-1
6,接下来,我们转到用户界面,如图6-1所示:
图6-1
7,在启动那里右键→添加对话框,如图7-1所示:
图7-1
8,这次,我们依次添加许可协议(图8-1),文本框(A)(图8-2),有需要的情况,可以再添加一个客户信息,这个看个人所需,这里就不再一一演示了.
图8-1
图8-2
9,然后我们先右键,把位置整理好来,如图9-1所示:
图9-1
10,现在我们来说说许可协议的方面,现在先新建一个word文档,然后在word文档里面写上一些字,这里就简单写了以下几个字:"
这个是一个许可协议,同意请按”下一步”,不同意请按”取消”.";
然后另存为:如图10-1所示:

图10-1
然后选择格式为rtf的文件保存起来,如图10-2所示:

图10-2
11,现在回到我们的程序里面,我们右键许可协议,选择属性,然后看到属性框这里,把刚刚我们编辑好的rtf文件弄进来,具体操作如图11-1,11-2,11-3所示:

图11-1

图11-2

图11-3,
单击"添加文件",然后选中刚刚您编辑好的rtf文件,确定即可,如图11-4所示:

图11-4;
12,现在我们来看看文本框的作用,首先给大家科普一下这 个文本框的作用.大家在安装软件或者配置网站的时候,往往都会做一些数据库配置的东西,因为不同的电脑,他的sql服务器名就不一样,有些有了实例名,那 就不能都默认为localhost了,还有用户名,密码什么的,每台机器都不一样,所以,这里我们就要用到文本框来帮我们录入这些将要安装部署我们网站的 一些信息了.ok,下面,我们就来操作这个文本框了,如图12-1所示,把该输进去的输好来,建议新手先按照我截图的来输,到后面自己熟练了再做修改,

图12-1
13,完成12的操作的时候,接下来,我们回到上面,添加一个新建项目,如图13-1所示:

图13-1
然后我们添加一个安装类,如图13-2所示:

图13-2
接下来,我们在新建好的类库里面,右键再添加一个新建项,如图13-3,所示:

图13-3
接下来,我们选择新建项为一个"安装程序类",如果找不到,可以在右上角的搜索框哪里输入"安装",搜索出来的结果第一个就是了,如图13-4所示:

图13-4
添加完了以后,我们就切换到代码模式,看看这个类有啥东东,来,一起gogogo,如图13-5所示:

图13-5
我们发现,他的代码很简洁,如图13-6所示:

图13-6
14,好嘞,做完这一步我们返回解决方案这边来,右键项目,添加项目输出,如图14-1所示:

图14-1
然后:项目选择---安装类,然后内容选择主输出,确定就好,如图14-2所示:

图14-2
15,接下来,我们就转到自定义操作这边来,如图15-1所示:

图15-1
然后右键添加自定义操作,如图15-2所示:

图15-2
然后双击进入"应用程序文件夹",如图15-3所示:

图15-3
然后选中"主输出来自安装类(活动)",点击确定即可,如图15-4所示:

图15-4
16,然后在去到这个的属性,如图所示,输入以下内容到相应位置,内容为(红色字体,黑色的双引号不要,但是红色的双引号要):"/dbname=[DBNAME] /server=[SERVER] /user=[UNAME] /pwd=[PWD] /targetdir="[TARGETDIR]\"",如图16-1所示:

图16-1
这里面就解释以下,刚刚各位应该还有印象我们之前添加了一个文本框吧?还记得文本框的内容么?嘿嘿,来,咱给你回忆一下,如图16-2所示:

图16-2
哈哈,想起来了吧,对了,这里面就是把文本框的值弄过来滴...等下再告诉你怎么用.好的,昨晚这一步了,我们要回到"安装类"里面来写一写代码了.(什么!!!这都还要写代码?!楼主,你这是要闹哪样~~~楼主回复:小盆友,代码不多滴,放心吧,呵呵)
17,现在我们回到安装类库下面的Installer1.cs,不会进入的就双击那个文件,然后点击切换到后台代码即可,先把代码补全,再慢慢给大伙解释哈,如图17-1所示;

图17-1
详细代码:
/// <summary>
/// 重写安装方法
/// </summary>
/// <param name="stateSaver"></param>
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string Server = Context.Parameters["server"].ToString();
string dbName = Context.Parameters["dbname"].ToString();
string userName = Context.Parameters["user"].ToString();
string userPass = Context.Parameters["pwd"].ToString();
string targetdir = Context.Parameters["targetdir"].ToString();
/*
* 设置webconfig连接字符串
*/
string webconfigpath = Path.Combine(this.Context.Parameters["targetdir"].ToString(), "web.config");
//修改第一个数据库连接
string webcofnigstring2 = File.ReadAllText(webconfigpath).Replace(@"server=JEFFREY9061\SQL2008;database=yd_esms;uid=sa;pwd=********", GetConnectionString2());
File.WriteAllText(webconfigpath, webcofnigstring2);
//修改第二个数据连接
string webcofnigstring = File.ReadAllText(webconfigpath).Replace(@"Data Source=JEFFREY9061\SQL2008;Initial Catalog=yd_esms;Persist Security Info=True;User ID=sa;Password=******", GetConnectionString());
File.WriteAllText(webconfigpath, webcofnigstring);
//这个是测试在安装目录下添加接收到的用户填写的数据库信息
File.WriteAllText(Path.Combine(targetdir, "log.txt"), Server + "/n/r" + dbName + "/n/r" + userName + "/n/r" + userPass);
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="connection"></param>
/// <param name="sql"></param>
void ExecuteSQL(SqlConnection connection, string sql)
{
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.ExecuteNonQuery();
}
/// <summary>
/// 获取文本框输入的信息,来插入到登录连接字符串
/// </summary>
/// <returns></returns>
private string GetConnectionString()
{
return @"Data Source=" + this.Context.Parameters["server"] + ";Initial Catalog=" + this.Context.Parameters["dbname"] + ";Persist Security Info=True;User ID=" + this.Context.Parameters["user"] + ";Password=" + this.Context.Parameters["pwd"] + "";
}
private string GetConnectionString2()
{
return @"server=" + this.Context.Parameters["server"] + ";database=" + this.Context.Parameters["dbname"] + ";uid=" + this.Context.Parameters["user"] + ";pwd=" + this.Context.Parameters["pwd"];
}
ok,现在我们来说说这个代码的意思:
第一个,方法,重写安装方法这个就一般都看得懂了,前面几个string都是获取文本框的值的,下面的就是获去web.config文件,然后修改里面的数据库连接字符串的,因为我用到两个连接字符串,所以写了两个,如果只有一个的,那就写一个即可,这个各取所需哈.
18, (奶奶的,还有?那么长,老子不看了...)其实我想告诉你,结束啦,现在,我们只需要把项目从头到尾(从上到下,先web,然后再安装类,然后再"一键 安装抄表系统")生成一边即可,生成完成后,我们就可以在项目右键安装即可.如图18-1所示:(什么!!!没有足够的权限不能运行?老兄,你可以右键, 然后在windows打开文件夹,右键以管理员模式运行即可,小儿科啦,相信你懂的.,如图18-2所示)

图18-1

图18-2
19:(怎么还有~~~,我的小宇宙要爆发啦...)小盆友们,别急,我下面给大家贴一贴图,看看安装过程而已,稍安勿躁哈...如图19-1到19-8所示:

图19-1

图19-2

图19-3(嘿嘿,这里就是刚刚的许可协议咯)

图19-4(这里就是刚刚我们添加的文本框啦,知道用处了吧)

图19-5(这里面选择好你的应用程序池哟.)
ps:此程序默认的安装路径是:C:\inetpub\wwwroot,然后访问的时候是:localhost:/xxx/即可,如图19-6和图19-7

图19-6

图19-7
看到这里,就完啦,如果觉得本文对您有所帮助,请点击右下角的"好文要顶"支持一下,谢谢;

OK,本次教程到此结束,还有其他的精彩之处,留给各位看官们自己发掘(提示:数据库相关问题,也是可以的,例如,导出一个sql文件,然后通过这个程序 读取您导出来的sql文件,然后再用语句执行就ok了,等等都可以拓展开来),如果对本文章有所意见或者建议,可以发邮件到jeffrey9061@live.cn或者支付回复本帖子互相学习.谢谢,转载请保留本字段本文地址:http://www.cnblogs.com/nangong/p/Web.html.