Step by step make your own webinstall installation package

  • 2020-05-17 05:15:39
  • OfStack

1. In order to better operate IIS, first add a class library (InstallClassLibrary) to the project. The attached code
 
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.AccessControl; 
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();// The following code specifies directory permissions for changing the site  
DirectoryInfo di = new DirectoryInfo("d:\\yourpath\\xml"); 
if((di.Attributes&FileAttributes.ReadOnly)!=0) 
di.Attributes=FileAttributes.Normal; 
DirectorySecurity ds=di.GetAccessControl(); 
ds.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE",FileSystemRights.Modify,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit, 
PropagationFlags.None,AccessControlType.Allow)); 
di.SetAccessControl(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; //  value  0  Indicates that the application is running within the process  1  Represents a value outside the process  2  Represents the process pool  
newRoot.Properties["AccessScript"][0] = true; //  Executable script  
newRoot.Invoke("AppCreate", true); 
newRoot.Properties["DefaultDoc"][0] = "login.aspx";// Set the start page  
newRoot.Properties["AppFriendlyName"][0] = "applicationName"; //  Application name  
newRoot.CommitChanges(); 
root.CommitChanges(); 
} 
catch (Exception ee) 
{ 
MessageBox.Show(" Virtual directory creation failed! You can create it manually!  " + ee.Message + ";" + ee.Source + ";" + ee.TargetSite + ";" + ee.InnerException + ";" + ee.StackTrace, "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); 
} 
} 
} 
} 

2. Add the installation project to the solution, and then add - project output to the installation project (Setup), adding the WEB content and the class library you just created to the directory.
3. Left click on the installation project to change the manufacturer, the title of the installation program, whether it is installed for all users, and the product name in the property window.
Right-click view - custom action, right-click install - add custom action - application folder, select "install class library (InstallClassLibrary)", after the addition is complete, right-click the application folder in the file system to set the default installation directory.
If you want to define more user installation data, add a user interface.

Related articles: