安装包修改web.config
安装包一些整理:
1:制作web安装程序选择web安装项目,而我没有选择web安装项目的原因是web安装项目制作出来的安装包是不可以选择软件的安装路径的,而安装项目制作出来的安装包是可以选择软件的安装目录的。
2:string dir = this.Context.Parameters["targetdir"].ToString(); 这个是获得网站安装的目录地址;
3:给安装的目录设置everyone权限操作(代码写在安装程序类中); 从targetdir获得安装后的地址然后设置权限;代码如下:
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
dir = this.Context.Parameters["targetdir"].ToString();
server = this.Context.Parameters["server"].ToString();
dbname = this.Context.Parameters["dbname"].ToString();
user = this.Context.Parameters["user"].ToString();
password = this.Context.Parameters["password"].ToString();
iis = this.Context.Parameters["iis"].ToString();
port = this.Context.Parameters["port"].ToString();
//移动数据库
//MoveData();
//创建虚拟目录
CreateVirtualDir();
////重写Config
WriteWebConfig();
////创建快捷方式
//CreateKJFS();
MessageBox.Show(dir);
string filename = dir+"Account"; //目标目录 这个文件夹ACCOUNT是在根目录下的;若要设置根目录只要dir就可以的;
string account = @"Everyone";//用户名
string userrights = @"F";//权限字符串,自己定义的
AddDirectorySecurity(filename, account, userrights);
}
static public void AddDirectorySecurity(string FileName, string Account, string UserRights)
{
FileSystemRights Rights = new FileSystemRights();
if (UserRights.IndexOf("R") >= 0)
{
Rights = Rights | FileSystemRights.Read;
}
if (UserRights.IndexOf("C") >= 0)
{
Rights = Rights | FileSystemRights.ChangePermissions;
}
if (UserRights.IndexOf("F") >= 0)
{
Rights = Rights | FileSystemRights.FullControl;
}
if (UserRights.IndexOf("W") >= 0)
{
Rights = Rights | FileSystemRights.Write;
}
bool ok;
DirectoryInfo dInfo = new DirectoryInfo(FileName);
if ((dInfo.Attributes & FileAttributes.ReadOnly) != 0)
{
dInfo.Attributes = FileAttributes.Normal;
}
DirectorySecurity dSecurity = dInfo.GetAccessControl();
InheritanceFlags iFlags = new InheritanceFlags();
iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, Rights, iFlags, PropagationFlags.None, AccessControlType.Allow);
dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, out ok);
dInfo.SetAccessControl(dSecurity);
}
4:修改web.config连接字符串的操作
//<configuration>
// <connectionStrings>
//<add name="配置文件中name的值"
// connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
// providerName="System.Data.SqlClient" />
//</connectionStrings>
#region 修改web.config的连接数据库的字符串
private void WriteWebConfig()
{
System.IO.FileInfo FileInfo = new System.IO.FileInfo(dir + "/web.config");
if (!FileInfo.Exists) //不存在web.config文件
{
throw new InstallException("没有找到web.config配置文件!");
}
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(FileInfo.FullName);
bool FoundIt = false;
foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
{
if (Node.Name == "add")
{
if (Node.Attributes.GetNamedItem("name").Value == "配置文件中name的值")
{
Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Persist Security Info=False;Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", server, dbname, user, password);
FoundIt = true;
}
}
}
if (!FoundIt)
{
throw new InstallException("修改web.config配置文件失败!");
}
xmlDocument.Save(FileInfo.FullName);
}
#endregion
5:
/dbname=[DBNAME] /password=[PASSWORD] /user=[USERNAME] /server=[DBSERVERNAME] /iis=[IISSERVER] /port=[PORT] /targetdir="[TARGETDIR]/"