因为有些脚本小子会注意到俺的网站,向网站文件夹里面搞一些乱七八糟的东西,或者传个木马上来,如果这样的话,俺会丢饭碗,会破产,所以俺用递归的方法写了个功能,来看看文件有没有给别人改动了,或者有不该存在的文件给传上来了,加密算法不写,因为怕人破解,只写遍历文件夹:
public static void SetAllSafeCode(string path)
{
path += "\\";
//if there is no Directories, only files exists
if (Directory.GetDirectories(path).Length == 0)
{
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
//initialize an safe string object
SafeString ss = new SafeString();
//set property of this object
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
//store this object into list
ssList.Add(ss);
}
return;
}
else//if this path contains Directory
{
//check all directory of this path
foreach (string name in Directory.GetDirectories(path))
{
SetAllSafeCode(name);//Recursion 递归
}
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
SafeString ss = new SafeString();
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
ssList.Add(ss);
}
}
}
public static void SetAllSafeCode(string path)
{
path += "\\";
//if there is no Directories, only files exists
if (Directory.GetDirectories(path).Length == 0)
{
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
//initialize an safe string object
SafeString ss = new SafeString();
//set property of this object
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
//store this object into list
ssList.Add(ss);
}
return;
}
else//if this path contains Directory
{
//check all directory of this path
foreach (string name in Directory.GetDirectories(path))
{
SetAllSafeCode(name);//Recursion 递归
}
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
SafeString ss = new SafeString();
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
ssList.Add(ss);
}
}
}