Thursday, February 1, 2007

Installer Class For .NET

/*


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration.Install;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using System.Web.Configuration;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.Protocols;
///
/// Summary description for InstallerClass
///
[RunInstaller(true)]
public class InstallerClass : System.Configuration.Install.Installer
{
private System.ComponentModel.Container components = null;
public InstallerClass()
{
components = new System.ComponentModel.Container();
}
protected override void OnBeforeInstall(IDictionary stateSaver)
{
//for Database Configuration parameter
string server = this.Context.Parameters["DATASOURCE"];
string DBName = this.Context.Parameters["DBNAME"];
string userId = this.Context.Parameters["USERID"];
string password = this.Context.Parameters["PASSWORD"];
//Connection string
string newConnectionString = "Data Source ="+ server +";Database="+ DBName +";User id="+ userId +";Password= " + password + "";
//Mail Settings Configuration
string sSenderId = this.Context.Parameters["SENDER"];
string sSMTPServer = this.Context.Parameters["SMTPSERVER"];
string sSMTPPort = this.Context.Parameters["SMTPPORT"];
//LDAP Path setting Configuration
string sDomainName = this.Context.Parameters["DOMAINNAME"];
string sAccessGroup = this.Context.Parameters["ACCESSGROUP"];
string sDomainPath = SetLDAPPath(sDomainName);
string sDeletePath = SetDeletePath(sDomainName);
string xmlConfigFile = "";
xmlConfigFile = Context.Parameters["INSTALLDIR"] + "web.config";
UpdateConfigFile(xmlConfigFile, newConnectionString, " ");
ModifyMailSettings(xmlConfigFile, sSenderId, sSMTPServer, sSMTPPort, "mailSettings");
ModifyDomainConfiguration(xmlConfigFile, sDomainPath, "DirectoryPath");
ModifyDomainConfiguration(xmlConfigFile, sDeletePath, "Deletepath");
ModifyDomainConfiguration(xmlConfigFile, sAccessGroup, "Group");
}
#region Update database Configuration
public void UpdateConfigFile(string filepath, string newValue, string keyName)
{
XmlReaderSettings xmlReaderSettings = null;
XmlDocument doc = null;
XmlReader xmlReader = null;
XmlWriterSettings xmlWriterSettings = null;
XmlWriter writer = null;
try
{
xmlReaderSettings = new XmlReaderSettings();
doc = new XmlDocument();
xmlReader = XmlReader.Create(filepath, xmlReaderSettings);
doc.Load(xmlReader);
xmlReaderSettings.CloseInput = true;
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (node.Name == "connectionStrings")
{
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.Name == "add" && childNode.Attributes["connectionStrings"] != null && childNode.Attributes["name"].Value == keyName)
{
string connStr = childNode.Attributes["connectionString"].Value.ToString();
string newConnStr = newValue;
childNode.Attributes["connectionString"].Value = newConnStr;
}
}
}
}
xmlReader.Close();
xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.CloseOutput = true;
writer = XmlWriter.Create(filepath, xmlWriterSettings);
doc.Save(writer);
}
catch (Exception)
{
//Handle exeptions
}
finally
{
writer.Close();
}
}
#endregion
#region Update Mail Settings
public void ModifyMailSettings(string sConfigfile, string sSender, string sSMTPServer,
string sSMTPPort, string sKeyName)
{
XmlReaderSettings xmlReaderSettings = null;
XmlDocument doc = null;
XmlReader xmlReader = null;
XmlWriterSettings xmlWriterSettings = null;
XmlWriter writer = null;
try
{
xmlReaderSettings = new XmlReaderSettings();
doc = new XmlDocument();
xmlReader = XmlReader.Create(sConfigfile, xmlReaderSettings);
doc.Load(xmlReader);
xmlReaderSettings.CloseInput = true;
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (node.Name == "system.net")
{
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.Name == sKeyName)
{
#region Write a Smtp Sender address
foreach (XmlNode smtpNode in childNode.ChildNodes)
{
if (smtpNode.Name == "smtp")
{
smtpNode.Attributes["from"].Value = sSender;
#region Write a SMTP Server Values
foreach (XmlNode NetworkNode in smtpNode.ChildNodes)
{
if (NetworkNode.Name == "network")
{
NetworkNode.Attributes["host"].Value = sSMTPServer;
NetworkNode.Attributes["port"].Value = sSMTPPort;
}
}
#endregion
}
}
#endregion
}
}
}

}
xmlReader.Close();
xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.CloseOutput = true;
writer = XmlWriter.Create(sConfigfile, xmlWriterSettings);
doc.Save(writer);
}
catch (Exception)
{
//Handle exeptions
}
finally
{
writer.Close();
}
}
#endregion
#region Update Domain Configuration
public void ModifyDomainConfiguration(string ConfigFile, string sldapPath, string keyName)
{
XmlReaderSettings xmlReaderSettings = null;
XmlDocument doc = null;
XmlReader xmlReader = null;
XmlWriterSettings xmlWriterSettings = null;
XmlWriter writer = null;
try
{
xmlReaderSettings = new XmlReaderSettings();
doc = new XmlDocument();
xmlReader = XmlReader.Create(ConfigFile, xmlReaderSettings);
doc.Load(xmlReader);
xmlReaderSettings.CloseInput = true;
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (node.Name == "connectionStrings")
{
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.Name == "add" && childNode.Attributes["connectionStrings"] != null && childNode.Attributes["name"].Value == keyName)
{
string connStr = childNode.Attributes["connectionString"].Value.ToString();
string newConnStr = sldapPath;
childNode.Attributes["connectionString"].Value = newConnStr;
}
}
}

}
xmlReader.Close();
xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.CloseOutput = true;
writer = XmlWriter.Create(ConfigFile, xmlWriterSettings);
doc.Save(writer);
}
catch (Exception)
{
//Handle exeptions
}
finally
{
writer.Close();
}
}
#endregion
#region SetLDAPPath Settings
private string SetLDAPPath(string sDomainName)
{
string[] sDNM = sDomainName.Split(Convert.ToChar(","));
string sPath ="LDAP://";
for (int icount = 0; icount < sDNM.Length; icount++)
{
sPath += "DC=" + sDNM[icount] + ",";
}
return sPath.Substring(0, sPath.Length - 1);
}
#endregion
#region Set Delete Path settings
private string SetDeletePath(string sDomainName)
{
string[] sDNM = sDomainName.Split(Convert.ToChar(","));
string sPath = "";
for (int icount = 0; icount < sDNM.Length; icount++)
{
sPath += "DC=" + sDNM[icount] + ",";
}
return sPath.Substring(0, sPath.Length - 1);
}
#endregion
}
*/