zl程序教程

您现在的位置是:首页 >  工具

当前栏目

一步步做自己的webinstall安装包

安装包 自己 一步步
2023-06-13 09:14:40 时间
1、为了能更好的操作IIS,先添加个类库(InstallClassLibrary)到项目中。附代码
复制代码代码如下:

usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Configuration.Install;
usingSystem.DirectoryServices;
usingSystem.Diagnostics;
usingSystem.Windows.Forms;
usingSystem.Security.AccessControl;
usingSystem.IO;
namespaceInstallClassLibrary
{
[RunInstaller(true)]
publicpartialclassWebInstaller:Installer
{
publicWebInstaller()
{
InitializeComponent();
}
publicoverridevoidInstall(IDictionarystateSaver)
{
base.Install(stateSaver);
CreateVirtualDir();//以下代码为更改网站指定目录权限
DirectoryInfodi=newDirectoryInfo("d:\\yourpath\\xml");
if((di.Attributes&FileAttributes.ReadOnly)!=0)
di.Attributes=FileAttributes.Normal;
DirectorySecurityds=di.GetAccessControl();
ds.AddAccessRule(newFileSystemAccessRule("NETWORKSERVICE",FileSystemRights.Modify,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit,
PropagationFlags.None,AccessControlType.Allow));
di.SetAccessControl(ds);
//
}
voidCreateVirtualDir()
{
try
{
DirectoryEntryroot=newDirectoryEntry("IIS://localhost/W3SVC/1/root");
DirectoryEntrynewRoot=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["AccessScript"][0]=true;//可执行脚本
newRoot.Invoke("AppCreate",true);
newRoot.Properties["DefaultDoc"][0]="login.aspx";//设置起始页
newRoot.Properties["AppFriendlyName"][0]="applicationName";//应用程序名
newRoot.CommitChanges();
root.CommitChanges();
}
catch(Exceptionee)
{
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)”,添加完成后,在文件系统中右键应用程序文件夹设置默认安装目录。
若还想去定义更多的用户安装数据,请增加用户界面。