接着上一节我们讲到的Install,我们接着来看第二步的实现。


在这个页面中检测的功能,主要是通过以下这段代码来实现检测功能,呵呵,还是用的字符串拼接输出,呵呵,晕没有办法,Discuz的程序员就是这样写的。》


1:  <%
2:       bool err = false;
3:       string result = SetupPage.InitialSystemValidCheck(ref err);
4:       Response.Write("<font color=red>"+result+"</font>");
5:  %>



具体的实现代码如下:


1:  public static string InitialSystemValidCheck(ref bool error)
2:  {
3:      error = false;
4:      StringBuilder sb = new StringBuilder();
5:      sb.Append("<table cellSpacing='0' cellPadding='0' width='90%' align='center' border='0'
6:                 bgcolor='#666666' style='font-size:12px'>");
7:
8:      HttpContext context = HttpContext.Current;
9:
10:      string filename = null;
11:      if (context != null)
12:          filename = context.Server.MapPath("/DNT.config");
13:      else
14:          filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DNT.config");
15:
16:      //系统BIN目录检查
17:      sb.Append(IISSystemBINCheck(ref error));
18:
19:
20:      //            GetRootDntconfigPath();
21:      //检查Dnt.config文件的有效性
22:      if (!GetRootDntconfigPath())
23:      {
24:          sb.Append("<tr style=\"height:15px\"><td bgcolor='#ffffff' width='5%'><img src='images/error.gif'
25:                     width='16' height='16'></td><td bgcolor='#ffffff' width='95%'> DNT.config
26:                     不可写或没有放置正确, 相关问题详见安装文档!</td></tr>");
27:          error = true;
28:      }
29:      else
30:      {
31:          sb.Append("<tr style=\"height:15px\"><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif'
32:                     width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>对 DNT.config 验证通过!</td>
33:                    </tr>");
34:      }
35:
36:
37:      //检查系统目录的有效性
38:      string folderstr = "admin,aspx,avatars,cache,config,editor,images,templates,upload";
39:      foreach (string foldler in folderstr.Split(','))
40:      {
41:          if (!SystemFolderCheck(foldler))
42:          {
43:              sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'>
44:                        </td><td bgcolor='#ffffff' width='95%'>对 " + foldler + " 目录没有写入和删除权限!</td>
45:                        </tr>");
46:              error = true;
47:          }
48:          else
49:          {
50:              sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif' width='16' height='16'>
51:                </td><td bgcolor='#ffffff' width='95%'>对 " + foldler + " 目录权限验证通过!</td></tr>");
52:          }
53:      }
54:      string filestr = "admin\\xml\\navmenu.config,javascript\\mymenu.js,install\\systemfile.aspx,upgrade
55:                       \\systemfile.aspx";
56:      foreach (string file in filestr.Split(','))
57:      {
58:          if (!SystemFileCheck(file))
59:          {
60:              sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'>
61:                        </td><td bgcolor='#ffffff' width='95%'>对 " + file.Substring(0,file.LastIndexOf('\\')) +
62:                        " 目录没有写入和删除权限!</td></tr>");
63:              error = true;
64:          }
65:          else
66:          {
67:              sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif' width='16' height='16'>
68:              </td><td bgcolor='#ffffff' width='95%'>对 " + file.Substring(0, file.LastIndexOf('\\')) +
69:              " 目录权限验证通过!</td></tr>");
70:          }
71:      }
72:
73:     if(!TempTest())
74:     {
75:         sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'>
76:                  </td><td bgcolor='#ffffff' width='95%'>您没有对 " + Path.GetTempPath() +
77:                  " 文件夹访问权限,详情参见安装文档.</td></tr>");
78:         error = true;
79:     }
80:     else
81:     {
82:      if (!SerialiazeTest())
83:      {
84:          sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'>
85:                     </td><td bgcolor='#ffffff' width='95%'>对config文件反序列化失败,详情参见安装文档.</td>
86:                    </tr>");
87:          error = true;
88:      }
89:      else
90:      {
91:          sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif' width='16' height='16'>
92:                 </td><td bgcolor='#ffffff' width='95%'>反序列化验证通过!</td></tr>");
93:      }
94:     }
95:      sb.Append("</table>");
96:
97:      return sb.ToString();
98:  }


在以上程序中,它首先会去检测DNT.Config文件的有效性


1:    string filename = null;
2:     if (context != null)
3:                  filename = context.Server.MapPath("/DNT.config");
4:     else
5:                  filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DNT.config");


接着再去检测IIS目录中的DLL是否存在的有效性:


1:   public static string IISSystemBINCheck(ref bool error)
2:   {
3:       string binfolderpath = HttpRuntime.BinDirectory;
4:
5:       string result = "";
6:       try
7:       {
8:           string[] assemblylist = new string[] { "Discuz.Aggregation.dll", "Discuz.Cache.dll",
9:                                                  "Discuz.Common.dll", "Discuz.Config.dll",
10:               "Discuz.Control.dll", "Discuz.Data.dll", "Discuz.Data.SqlServer.dll","Discuz.Entity.dll",
11:               "Discuz.Event.dll", "Discuz.Forum.dll",
12:               "Discuz.Install.dll", "Discuz.Plugin.dll","Discuz.Plugin.Preview.Jpg.dll",
13:               "Discuz.Plugin.Spread.dll", "Discuz.Web.Admin.dll",
14:               "Discuz.Web.dll", "Discuz.Web.Services.dll","Interop.SQLDMO.dll","Newtonsoft.Json.dll" };
15:           bool isAssemblyInexistence = false;
16:           ArrayList inexistenceAssemblyList = new ArrayList();
17:           foreach (string assembly in assemblylist)
18:           {
19:               if (!File.Exists(binfolderpath + assembly))
20:               {
21:                   isAssemblyInexistence = true;
22:                   error = true;
23:                   inexistenceAssemblyList.Add(assembly);
24:               }
25:           }
26:           if (isAssemblyInexistence)
27:           {
28:               foreach (string assembly in inexistenceAssemblyList)
29:               {
30:                       result += "<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif'
31:                                 width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>" + assembly +
32:                                " 文件放置不正确<br/>请将所有的dll文件复制到目录 " + binfolderpath +
33:                                " 中.</td></tr>";
34:               }
35:           }
36:       }
37:       catch
38:       {
39:           result += "<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'>
40:                     </td><td bgcolor='#ffffff' width='95%'>请将所有的dll文件复制到目录 " + binfolderpath
41:                      + " 中.</td></tr>";
42:           error = true;
43:       }
44:
45:       return result;
46:   }
在以上代码中用到了一些不常用的类,比如HttpRuntime类,我们过HttpRuntime这个类中的BinDirectory这个属性找到WEB程序所用到的bin目录,
之后用IO中的File.Exist来检测这个文件是否存在,不过我再一次的问一下,难道Discuz的程序员不知道@号的存在???,再一个就是对StringBuilder
在程序中的性能问题表示怀疑。




再接下来是检测Dnt.config文件的有效性:


1:   public static bool GetRootDntconfigPath()
2:   {
3:       try
4:       {
5:
6:           HttpContext context = HttpContext.Current;
7:           string webconfigfile = "";
8:           if (!Utils.FileExists(webconfigfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DNT.config"))
9:               && (!Utils.FileExists(webconfigfile = Path.Combine(context.Request.PhysicalApplicationPath, "dnt.config")))
10:               && (!Utils.FileExists(webconfigfile = Utils.GetMapPath("../dnt.config")))
11:               && (!Utils.FileExists(webconfigfile = Utils.GetMapPath("http://www.cnblogs.com/dnt.config")))
12:               && (!Utils.FileExists(webconfigfile = Utils.GetMapPath("http://www.cnblogs.com/../dnt.config"))))
13:           {
14:               return false;
15:           }
16:           else
17:           {
18:               StreamReader sr = new StreamReader(webconfigfile);
19:               string content = sr.ReadToEnd();
20:               sr.Close();
21:               content += " ";
22:               StreamWriter sw = new StreamWriter(webconfigfile, false);
23:               sw.Write(content);
24:               sw.Close();
25:               return true;
26:           }
27:       }
28:       catch
29:       {
30:           return false;
31:       }
32:   }
在这个检测的过程中用到了Discuz.Common中的Utils.cs这个类,这个类中主要是提供了一些工具类的支持,呵呵。其中有一些真的是不错的,以后可以
拿来借鉴一下。



为了方便 大家理解我做了一张思维图,供大家分析。


其中的SerialiazeTest()成员方法中用到了序列化的功能。在下一小节中再继续分析。