vs2008制作web安装程序
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Diagnostics;
using System.Security.AccessControl;
using System.IO;
namespace testInstall
{
[RunInstaller(true)]
public partial class myInstall : Installer
{
public myInstall()
{
InitializeComponent();
}
//bool b_chk_webapp = true;
//bool b_chk_timer = true;
//bool b_chk_mysql = true;
//bool b_chk_database = true;
//string ls_servername = "", ls_user = "", ls_password = "", ls_databasename = "",dir="";
/// <summary>
/// [targetdir]=E:\West.Co\web系统\碧彩\
/// [targetdir1]=E:\\West.Co\\web系统\\碧彩\\
/// [targetdir2]=E:/West.Co/web系统/碧彩/
/// </summary>
/// <param name="stateSaver"></param>
public override void Install(IDictionary stateSaver)
{
try
{
base.Install(stateSaver);
string dir = this.Context.Parameters["targetdir"].ToString();
//b_chk_webapp = this.Context.Parameters["CHK_WEBAPP"].ToString()=="1"?true:false;
//b_chk_timer = this.Context.Parameters["CHK_TIMER"].ToString() == "1" ? true : false;
//b_chk_mysql = this.Context.Parameters["CHK_MYSQL"].ToString() == "1" ? true : false;
//b_chk_database = this.Context.Parameters["CHK_DATABASE"].ToString() == "1" ? true : false;
//ls_servername = this.Context.Parameters["SERVERNAME"].ToString();
//ls_user = this.Context.Parameters["USER"].ToString();
//ls_password = this.Context.Parameters["PASSWORD"].ToString();
//ls_databasename = this.Context.Parameters["DATABASENAME"].ToString();
string ls_temp = "", ls_path = "";
#region 配置文件
ls_path = dir + @"Resource\config\systemcfg.xml";
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir).Replace("[targetdir1]", dir.Replace("\\", "\\\\"));
FileSystem.Create(ls_path, ls_temp, false);//修改文件
#endregion
#region 服务
ls_path = dir + @"bizerbaSrv\uninstallsrv.bat";
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
ls_path = dir + @"bizerbaSrv\install.bat";
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
ProcessStartInfo start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
#endregion
#region MySql
//--------修改my.ini文件
string ls_mypath = dir + @"MySql\MySQL Server 5.1\my.ini";
string ls_mycntpath = dir.Replace("\\", "/");
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_mypath);
ls_temp = ls_temp.Replace("[targetdir2]", ls_mycntpath);
FileSystem.Create(ls_mypath, ls_temp, false);//修改文件
//--------修改my.ini文件
//---------注册mysql服务到服务列表中
ls_path = dir + @"MySql\MySQL Server 5.1\bin\unstall.bat";
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
ls_path = dir + @"MySql\MySQL Server 5.1\bin\install.bat";
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
//---------注册mysql服务到服务列表中
#endregion
#region 数据库
ls_path = dir + @"pre_restore.bat";
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
System.Threading.Thread.Sleep(5000);
ls_path = dir + @"restore_bizerba.bat";
//修改文件必要参数
ls_temp = FileSystem.ReadFileString(ls_path);
ls_temp = ls_temp.Replace("[targetdir]", dir);
FileSystem.Create(ls_path, ls_temp, false);//修改文件
start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
#endregion
#region 目录访问权限
string[] ls_paths = new string[]{dir + @"Resource\config",
dir+@"Resource\config\systemcfg.xml",
dir+@"Resource\config\en.xml",
dir + @"Resource\config\ctrs.xml",
dir + @"Resource\config\chs.xml",
dir + @"Resource\data"};
FileSystem.CreateOrAppend("c:\\log.txt", ls_paths.Length.ToString());
for (int i = 0; i < ls_paths.Length; i++)
{
FileSystem.CreateOrAppend("c:\\log.txt", ls_paths.Length.ToString() + ":" + ls_paths[i]);
setRight(ls_paths[i]);
}
#endregion
}
catch (Exception ex)
{
FileSystem.CreateOrAppend("c:\\log.txt", ex.Message.ToString());
}
}
/// <summary>
/// 设置文件夹、文件的ntfs权限
/// </summary>
/// <param name="ls_path"></param>
private void setRight(string ls_path)
{
DirectoryInfo di = new DirectoryInfo(ls_path);
DirectorySecurity ds = di.GetAccessControl();
try
{
FileSystemAccessRule newAccessRule = new FileSystemAccessRule("ASPNET", FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
ds.AddAccessRule(newAccessRule);
di.SetAccessControl(ds);
}
catch(Exception ex)
{
FileSystem.CreateOrAppend("c:\\log.txt", ex.Message.ToString());
}
try
{
FileSystemAccessRule newAccessRule = new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
ds.AddAccessRule(newAccessRule);
di.SetAccessControl(ds);
}
catch (Exception ex)
{
FileSystem.CreateOrAppend("c:\\log.txt", ex.Message.ToString());
}
}
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
string dir = this.Context.Parameters["targetdir"].ToString();
string ls_path = "";
#region 服务
ls_path = dir + @"bizerbaSrv\uninstallsrv.bat";
//修改文件必要参数
ProcessStartInfo start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
#endregion
#region 卸载mySql服务
ls_path = dir + @"MySql\MySQL Server 5.1\bin\unstall.bat";
start = new ProcessStartInfo(ls_path, @"-default -mscc -word -c:/matc.dll -value");
Process.Start(start);
#endregion
}
}
}