<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>检测虚拟目录</title>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<style rel='stylesheet'>
body, div, fieldset, legend, a {
	color:		#666666;
	font-family:	tahoma, verdana, 'courier new', sans-serif;
	font-size:		12px !important;
	font-size:		11px;
	line-height:	18px;
	background:		white;
}
a	{
	color:		#2C78C5;
	text-decoration:none;
}
a:hover	{
	color:		#ff6600;
	text-decoration:underline;
}
</style>
</head>
<body style='margin-top:12px; text-align:center' oncontextmenu='return false'>
&nbsp; &nbsp;
<span style='position:relative; width:350px; text-align:left; top:9px; font-weight:bold;'>
	<span style='background:white; padding:2px'>检测结果</span>
</span>
<div style='width:350px; margin:auto; text-align:left; padding:12px; border:1px #c3c3c3 solid; '>
<%
	Response.Write("<br />");

	string[] assemblies = new string[] {"Webdisk.dll", "Mytaoyuan.Com.Upload.dll", "ICSharpCode.SharpZipLib.dll", "Interop.Scripting.dll"};
	string binPath      = HttpRuntime.BinDirectory; 
	string filePath     = Request.PhysicalPath.Substring(0, Request.PhysicalPath.LastIndexOf("\\") + 1);
	string[] machineCfgPathArr = HttpRuntime.MachineConfigurationDirectory.Split('\\');
	string CLRVersion = machineCfgPathArr[machineCfgPathArr.Length-2];
	string testUrl  = Request.Url.ToString();
	       testUrl  = testUrl.Substring(0, testUrl.LastIndexOf("/") + 1);

	string testPath = Server.MapPath("~/");
	string testFile = "directory_accessibility_test.txt";
	string testWord = "'" + testPath + "' was successfully accessed.\r\n\r\nat " + DateTime.Now.ToString("g");

	string appName = "《桃源网络硬盘.Net》";

	string CLRIsOK  = ".NET Framework版本为{0}，检测通过。";
	string CLRNotOK = appName + "要求.NET Framework版本为v1.1.4322或v2.0.50727，您所安装的版本为{0}，可能无法正常运行。<br /><span style='color:red'>请下载并安装</span> <a href='http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe'>dotnetfx2.0</a>(22M)。";

	string binIsOK  = "IIS设置与bin目录放置正确，检测通过。";
	string moveBin  = "该检测未通过。<br /><span style='color:red'>请照安装说明中将 " + filePath + "目录做成虚拟目录。</span>";

	string canWrite     = "存放数据的目录已具有权限，检测通过。";
	string cannotWrite  = "该检测未通过，<span style='color:red'>" + testPath + "</span>的权限不足。需要<span style='color:red'>在该目录属性的安全选项卡中，添加.NET进程用户(默认Win2K为ASPNET用户，Win2003为IIS_WPG用户组)，和users用户，并且设置为完全权限。</span>";

	string result    = "所有检测项均已通过。";
	string exception = "请按上述红色字的说明进行操作，再重新检测。";


	//Test Root Info
	Response.Write("<span style='font-family:tahoma'>●</span> <span style='color:#000000'>被检测的Web应用程序目录:</span><br />" + testUrl);


	//Test .NET Framework
	Response.Write("</br /></br /><span style='font-family:tahoma'>●</span> <span style='color:#000000'>.NET Framework检测:</span><br />");
	if (string.Compare(CLRVersion, "v1.1.4322") == 0)
	{
		Response.Write(string.Format(CLRIsOK, CLRVersion));
	}
	else
	{
		if (string.Compare(CLRVersion, "v2.0.50727") == 0)
		{
			Response.Write(string.Format(CLRIsOK, CLRVersion));
		}
		else
		{
			result = exception;
			Response.Write(string.Format(CLRNotOK, CLRVersion));		
		}
	}


	//Test IIS Virtual Address
	Response.Write("</br /></br /><span style='font-family:tahoma'>●</span> <span style='color:#000000'>IIS与bin目录检测:</span><br />");
	try
	{
		bool allFound = true;
		string notExists = string.Empty;
		foreach(string assembly in assemblies)
		{
			if (!File.Exists(binPath + assembly))
			{
				allFound   = false;
				notExists += "文件 " + binPath + assembly + " 未找到<br />";
			}
		}
		if (allFound)
		{
			Response.Write( binIsOK );
		}
		else
		{
			result = exception;
			Response.Write( notExists + moveBin );
		}
	}
	catch
	{
		result = exception;
		Response.Write(moveBin);
	}


	//Test AccessWrite of testPath ("/files/localfiles")
	Response.Write("</br /></br /><span style='font-family:tahoma'>●</span> <span style='color:#000000'>数据目录权限检测:</span><br />");
	try
	{
		using (StreamWriter sw = new StreamWriter(testPath + testFile, false, Encoding.GetEncoding("gb2312")))
		{
			sw.Write( testWord );
		}
		Response.Write( canWrite );
	}
	catch
	{
		result = exception;
		Response.Write( cannotWrite );
	}
	string accessDbPath = ConfigurationSettings.AppSettings["conn"];
	if (accessDbPath.Split(';').Length < 4 && accessDbPath.Split('=').Length < 4)
	{
		if (!Regex.IsMatch(accessDbPath, @"^\s*[a-zA-Z]{1,10}:.*$"))
		{
			accessDbPath = Server.MapPath(accessDbPath);
		}
		if (!File.Exists(accessDbPath))
		{
			Response.Write("<br /><span style='color:red'>Access数据库 " + accessDbPath + " 不存在</span><br />");
		}
		else
		{
			OleDbConnection conn = new OleDbConnection();
			conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + accessDbPath;
			conn.Open();
			string sql = "insert into message (title) values ('test')";
			try {
				OleDbCommand cmd = new OleDbCommand(sql, conn);
				cmd.ExecuteNonQuery();
			}
			catch (OleDbException e) {
				Response.Write("<br /><span style='color:red'>向access数据库插入测试数据时失败，请检查数据库文件权限。<br />" + e.Message);
			}
			conn.Close();
		}
	}


	//Terminate
	Response.Write("<br /><br /><strong style='color:black'>" + result + "</strong>");
	Response.Write("<div style='width:100%; text-align:right; margin-top:15px; padding-top:3px; border-top:1px #cccccc solid'><a href='http://www.mytaoyuan.com' target='_blank'>mytaoyuan.com</a> &nbsp; <a href='javascript:window.close()'>[关闭窗口]</a></div>");
	Response.Write("\r\n</body>\r\n</html>");
	Response.End();
%>
	<br />
	未安装.NET Framework。
	<br />
	请下载并安装该文件: <a href='http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe'>dotnetfx2.0</a>。
	<div style='width:100%; text-align:right; margin-top:15px; padding-top:3px; border-top:1px #cccccc solid'>
		Created by <a href='http://www.mytaoyuan.com' target='_blank'>mytaoyuan.com</a>
		&nbsp;
		<a href='javascript:window.close()'>[关闭窗口]</a>	</div>
</div>
</body>
</html>