简单站点部署工具
最近发现给客户部署Web应用的时候,总是来回跑,麻烦,于是参考了网上的例子,写了小的站点部署工具给客户用,操作很简单,他自己就可以高低,其实功能很简单,思路也很简单,就是几个小功能拼凑。总共分三步:
实际上就是几个小过程的综合:站点设置,复制文件,附加数据库。
站点设置:
Code
const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
string virtualDirName = txtWebName.Text;//虚拟目录名称
string physicalPath = txtServerPath.Text + "\\Web";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);
tbEntry.Properties["Path"][0] = physicalPath;
tbEntry.Invoke("AppCreate", true);
tbEntry.Properties["AccessScript"][0] = false;//脚本可执行
tbEntry.Properties["AccessRead"][0] = true;
tbEntry.Properties["ContentIndexed"][0] = true; ;
tbEntry.Properties["DefaultDoc"][0] = "Index.aspx,Default.aspx";
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
tbEntry.Properties["AccessScript"][0] = true;
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
string virtualDirName = txtWebName.Text;//虚拟目录名称
string physicalPath = txtServerPath.Text + "\\Web";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);
tbEntry.Properties["Path"][0] = physicalPath;
tbEntry.Invoke("AppCreate", true);
tbEntry.Properties["AccessScript"][0] = false;//脚本可执行
tbEntry.Properties["AccessRead"][0] = true;
tbEntry.Properties["ContentIndexed"][0] = true; ;
tbEntry.Properties["DefaultDoc"][0] = "Index.aspx,Default.aspx";
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
tbEntry.Properties["AccessScript"][0] = true;
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
复制文件:
Code
/// <summary>
/// 遍历文件目录
/// </summary>
public bool ListFiles(FileSystemInfo info)
{
bool chk = false;
try
{
if (!info.Exists)
{
return chk;
}
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null)
{
return chk;
}
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//如果是文件,则移动文件
if (file != null)
{
MoveFile(file.FullName, file.Name);
string filename = file.FullName;
txtMsg.AppendText("移动"+filename + "完毕!\r\n");
Application.DoEvents();
}
else //对于子目录,进行递归调用
{
//先检查目录存在情况
CreateFolder(files[i].FullName);
//递归
ListFiles(files[i]);
}
}
chk = true;
}
catch
{
chk = false;
}
return chk;
}
//移动文件
private void MoveFile(string fullName,string fileName)
{
string Path_From = "";
string Path_To = "";
Path_From = fullName.Replace(fileName, "");
if (Path_From.IndexOf("DataBase") != -1 && chkPath.Checked)
{
Path_To = Path_From.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtDataPath.Text);
}
else
{
Path_To = Path_From.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtServerPath.Text);
}
if (!Directory.Exists(Path_To))
{
Directory.CreateDirectory(Path_To);
}
File.Copy(fullName, Path.Combine(Path_To,fileName),true);
}
//检查目录
private void CreateFolder(string path)
{
string folder = "";
if (path.IndexOf("DataBase") != -1 && chkPath.Checked)
{
folder = path.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtDataPath.Text);
}
else
{
folder = path.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtServerPath.Text);
}
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
}
/// <summary>
/// 遍历文件目录
/// </summary>
public bool ListFiles(FileSystemInfo info)
{
bool chk = false;
try
{
if (!info.Exists)
{
return chk;
}
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null)
{
return chk;
}
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//如果是文件,则移动文件
if (file != null)
{
MoveFile(file.FullName, file.Name);
string filename = file.FullName;
txtMsg.AppendText("移动"+filename + "完毕!\r\n");
Application.DoEvents();
}
else //对于子目录,进行递归调用
{
//先检查目录存在情况
CreateFolder(files[i].FullName);
//递归
ListFiles(files[i]);
}
}
chk = true;
}
catch
{
chk = false;
}
return chk;
}
//移动文件
private void MoveFile(string fullName,string fileName)
{
string Path_From = "";
string Path_To = "";
Path_From = fullName.Replace(fileName, "");
if (Path_From.IndexOf("DataBase") != -1 && chkPath.Checked)
{
Path_To = Path_From.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtDataPath.Text);
}
else
{
Path_To = Path_From.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtServerPath.Text);
}
if (!Directory.Exists(Path_To))
{
Directory.CreateDirectory(Path_To);
}
File.Copy(fullName, Path.Combine(Path_To,fileName),true);
}
//检查目录
private void CreateFolder(string path)
{
string folder = "";
if (path.IndexOf("DataBase") != -1 && chkPath.Checked)
{
folder = path.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtDataPath.Text);
}
else
{
folder = path.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ResourceFiles", txtServerPath.Text);
}
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
}
附加数据库:
Code
/// <summary>
/// 附加数据库
/// </summary>
private bool AttachDataBase()
{
bool check;
labMsg.Text = "正在附加数据库";
Application.DoEvents();
try
{
string sql = string.Empty;
if (File.Exists(txtData.Text))
{
sql = "EXEC sp_attach_db @dbname = '" + txtDataBaseName.Text +
"', @filename1 = '" + txtData.Text +
"',@filename2='" + txtLog.Text + "'";
SqlConnection conn = new SqlConnection(string.Format("Data Source={0};Initial Catalog=master;User ID={1};PWD={2}", "(local)", "sa", "sa"));
using (conn)
{
lock (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
}
check = true;
}
catch
{
check = false;
}
labMsg.Text = "数据库附件完毕";
return check;
}
/// <summary>
/// 附加数据库
/// </summary>
private bool AttachDataBase()
{
bool check;
labMsg.Text = "正在附加数据库";
Application.DoEvents();
try
{
string sql = string.Empty;
if (File.Exists(txtData.Text))
{
sql = "EXEC sp_attach_db @dbname = '" + txtDataBaseName.Text +
"', @filename1 = '" + txtData.Text +
"',@filename2='" + txtLog.Text + "'";
SqlConnection conn = new SqlConnection(string.Format("Data Source={0};Initial Catalog=master;User ID={1};PWD={2}", "(local)", "sa", "sa"));
using (conn)
{
lock (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
}
check = true;
}
catch
{
check = false;
}
labMsg.Text = "数据库附件完毕";
return check;
}
前提是系统装有.net Framework2.0,数据库SqlServer,IIS.
本博客文章版权归博客园和solan3000共同所有。