一步步做自己的webinstall安装包
网络编程 2021-07-04 22:40www.168986.cn编程入门
众所周知,在VS环境下自带的WEBINSTALL项目无法选择安装路径,这让很多开发者头痛不已。现提供一种办法供大家参考
1、 为了能更好的操作IIS,先添加个类库(InstallClassLibrary)到项目中。附代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.DirectoryServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.AessControl;
using System.IO;
namespace InstallClassLibrary
{
[RunInstaller(true)]
public partial class WebInstaller : Installer
{
public WebInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
CreateVirtualDir();//以下代码为更改网站指定目录权限
DirectoryInfo di = new DirectoryInfo("d:\\yourpath\\xml");
if((di.Attributes&FileAttributes.ReadOnly)!=0)
di.Attributes=FileAttributes.Normal;
DirectorySecurity ds=di.GetAessControl();
ds.AddAessRule(new FileSystemAessRule("NETWORK SERVICE",FileSystemRights.Modify,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit,
PropagationFlags.None,AessControlType.Allow));
di.SetAessControl(ds);
//
}
void CreateVirtualDir()
{
try
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
DirectoryEntry newRoot = root.Children.Add("virtualName", root.SchemaClassName);
newRoot.Properties["Path"][0] = "d:\\yourpath"; //this.Context.Parameters["targetdir"];
newRoot.Properties["AppIsolated"][0] = 2; // 值 0 表示应用程序在进程内运行,值 1 表示进程外,值 2 表示进程池
newRoot.Properties["AessScript"][0] = true; // 可执行脚本
newRoot.Invoke("AppCreate", true);
newRoot.Properties["DefaultDoc"][0] = "login.aspx";//设置起始页
newRoot.Properties["AppFriendlyName"][0] = "applicationName"; // 应用程序名
newRoot.CommitChanges();
root.CommitChanges();
}
catch (Exception ee)
{
MessageBox.Show("虚拟目录创建失败!您可以手动创建! " + ee.Message + ";" + ee.Source + ";" + ee.TargetSite + ";" + ee.InnerException + ";" + ee.StackTrace, "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
}
}
2、添加安装项目到解决方案中,然后在该安装项目(Setup)中添加-项目输出,把WEB内容和刚才建立的类库添加到目录中。
3、在安装项目左键在属性窗口中更改制造商,安装程序的标题、是否针对所有用户安装、产品名等内容。
右键-视图-自定义操作,右键安装-添加自定义操作-应用程序文件夹,选择“安装类库(InstallClassLibrary)”,添加完成后,在文件系统中右键应用程序文件夹设置默认安装目录。
若还想去定义更多的用户安装数据,请增加用户界面。
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.DirectoryServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.AessControl;
using System.IO;
namespace InstallClassLibrary
{
[RunInstaller(true)]
public partial class WebInstaller : Installer
{
public WebInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
CreateVirtualDir();//以下代码为更改网站指定目录权限
DirectoryInfo di = new DirectoryInfo("d:\\yourpath\\xml");
if((di.Attributes&FileAttributes.ReadOnly)!=0)
di.Attributes=FileAttributes.Normal;
DirectorySecurity ds=di.GetAessControl();
ds.AddAessRule(new FileSystemAessRule("NETWORK SERVICE",FileSystemRights.Modify,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit,
PropagationFlags.None,AessControlType.Allow));
di.SetAessControl(ds);
//
}
void CreateVirtualDir()
{
try
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
DirectoryEntry newRoot = root.Children.Add("virtualName", root.SchemaClassName);
newRoot.Properties["Path"][0] = "d:\\yourpath"; //this.Context.Parameters["targetdir"];
newRoot.Properties["AppIsolated"][0] = 2; // 值 0 表示应用程序在进程内运行,值 1 表示进程外,值 2 表示进程池
newRoot.Properties["AessScript"][0] = true; // 可执行脚本
newRoot.Invoke("AppCreate", true);
newRoot.Properties["DefaultDoc"][0] = "login.aspx";//设置起始页
newRoot.Properties["AppFriendlyName"][0] = "applicationName"; // 应用程序名
newRoot.CommitChanges();
root.CommitChanges();
}
catch (Exception ee)
{
MessageBox.Show("虚拟目录创建失败!您可以手动创建! " + ee.Message + ";" + ee.Source + ";" + ee.TargetSite + ";" + ee.InnerException + ";" + ee.StackTrace, "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
}
}
2、添加安装项目到解决方案中,然后在该安装项目(Setup)中添加-项目输出,把WEB内容和刚才建立的类库添加到目录中。
3、在安装项目左键在属性窗口中更改制造商,安装程序的标题、是否针对所有用户安装、产品名等内容。
右键-视图-自定义操作,右键安装-添加自定义操作-应用程序文件夹,选择“安装类库(InstallClassLibrary)”,添加完成后,在文件系统中右键应用程序文件夹设置默认安装目录。
若还想去定义更多的用户安装数据,请增加用户界面。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指