예제 #1
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        public static string ReadFileStr(string vpath, bool lines = false)
        {
            FileEncodeHelper fileEncodeHelper = new FileEncodeHelper();

            vpath = vpath.ToLower();
            if (Enumerable.Count <string>((IEnumerable <string>)BanRead, (Func <string, bool>)(p => vpath.IndexOf(p) > -1)) > 0)
            {
                throw new Exception("该文件不允许读取!");
            }
            string str1 = IOPath.VToP(vpath);

            if (!File.Exists(str1))
            {
                throw new Exception(vpath + "文件不存在!");
            }
            if (!lines)
            {
                return(File.ReadAllText(str1, fileEncodeHelper.GetType(str1)));
            }
            string str2 = "";

            foreach (string str3 in File.ReadLines(str1, fileEncodeHelper.GetType(str1)))
            {
                str2 = str2 + str3 + "<br/>";
            }
            return(str2);
        }
예제 #2
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 移除文件或目录
        /// </summary>
        /// <param name="vpath">文件或目录虚拟路径</param>
        /// <returns>true:删除成功</returns>
        public static bool DelFile(string vpath)
        {
            if (string.IsNullOrEmpty(vpath))
            {
                return(false);
            }
            vpath = PathDeal(vpath).ToLower();
            string path = IOPath.VToP(vpath);

            //是否禁止删除
            foreach (string ban in BanDelDir)
            {
                if (vpath.StartsWith(ban))
                {
                    return(false);
                }
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            else if (Directory.Exists(path))
            {
                DirectoryInfo   dir    = new DirectoryInfo(path);
                DirectoryInfo[] childs = dir.GetDirectories();
                foreach (DirectoryInfo child in childs)
                {
                    child.Delete(true);
                }
                dir.Delete(true);
            }
            return(true);
        }
예제 #3
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        public static string WriteFile(string vpath, string content)
        {
            string name = "";
            string path = "";

            IOPath.GetNameAndPath(vpath, out name, out path);
            return(WriteFile(path, name, content));
        }
예제 #4
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 如不存在,则创建目录
        /// </summary>
        /// <param name="vpath">虚拟目录路径,不包含文件名</param>
        public static void CreateDir(string vpath)
        {
            string ppath = IOPath.VToP(vpath);

            while (ppath.EndsWith("\\"))
            {
                ppath = ppath.TrimEnd('\\');
            }
            int num = ppath.LastIndexOf("\\") + 1;

            CreateDir(ppath.Substring(0, num), ppath.Substring(num, ppath.Length - num));
        }
예제 #5
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        //----------------下载文件|字符串
        public static void DownFile(string vpath, string fname = "")
        {
            vpath = PathDeal(vpath);
            string str    = IOPath.VToP(vpath);
            string exName = Path.GetExtension(vpath).ToLower();

            if (Enumerable.Count <string>((IEnumerable <string>)BanDownName, (Func <string, bool>)(p => p.Equals(exName))) != 0 || !File.Exists(str))
            {
                return;
            }
            DownLoadFile(str, fname, Encoding.UTF8);
        }
예제 #6
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 保存文件(限定保存目录)
        /// </summary>
        public static string SaveFile(string vpath, string fileName, byte[] file)
        {
            DirPathDel(ref vpath);
            if (FileNameCheck(fileName) || !VPathCheck(vpath))
            {
                throw new Exception(vpath + fileName + "保存失败,不符合命名规范!");
            }
            string dirPath = IOPath.VToP(vpath.TrimEnd('/') + "/");

            CreateDir(dirPath, "");
            File.WriteAllBytes(dirPath + fileName, file);
            return(IOPath.PToV(dirPath + fileName));
        }
예제 #7
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 该路径的根目录是否允许写入
        /// </summary>
        /// <param name="vpath">保存的虚拟路径</param>
        /// <returns>true:检测通过</returns>
        public static bool VPathCheck(string vpath)
        {
            string root = IOPath.GetPathRoot(vpath);

            for (int index = 0; index < BanPathName.Length; ++index)
            {
                if (BanPathName[index].Equals(root))
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #8
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 允许写入JS与html,config等文件
        /// </summary>
        /// <param name="vpath"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string WriteUnSafeFile(string vpath, string content)
        {
            string name = "";
            string path = "";

            IOPath.GetNameAndPath(vpath, out name, out path);
            DirPathDel(ref vpath);
            string dirPath = IOPath.VToP(vpath);

            //if (SafeC.FileNameCheck(new string[1] { fileName }) || !SafeC.VPathCheck(vpath)) { throw new Exception(vpath + fileName + "保存失败,不符合命名规范!"); }
            CreateDir(dirPath, "");
            string ppath = dirPath + name;

            byte[] bytes = Encoding.UTF8.GetBytes(content);
            File.WriteAllBytes(ppath, bytes);
            return(IOPath.PToV(ppath));
        }
예제 #9
0
파일: SafeC.cs 프로젝트: baixue001/IPS
 /// <summary>
 /// 更新Cert,如果不存在则新建
 /// </summary>
 public static void Cert_Update()
 {
     if (!File.Exists(IOPath.VToP(certPath)))
     {
         certKey = Cert_NoExistThenNew();
     }
     try
     {
         string key = ReadFileStr(certPath);
         if (!string.IsNullOrEmpty(key))
         {
             key = EncryHelper.DesDecrypt(key);
         }
         else
         {
             key = "";
         }
         certKey = key;
     }
     catch { certKey = ""; }
 }
예제 #10
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        public static byte[] ReadFileByte(string vpath)
        {
            vpath = vpath.ToLower();
            if (Enumerable.Count <string>((IEnumerable <string>)BanRead, (Func <string, bool>)(p => vpath.IndexOf(p) > -1)) > 0)
            {
                throw new Exception("该文件不允许读取!");
            }
            string path = IOPath.VToP(vpath);

            if (!File.Exists(path))
            {
                throw new Exception(vpath + "文件不存在!");
            }
            using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                fileStream.Close();
                return(buffer);
            }
        }
예제 #11
0
파일: SafeC.cs 프로젝트: baixue001/IPS
        /// <summary>
        /// 创建Cert并更新Key
        /// </summary>
        public static string Cert_NoExistThenNew()
        {
            string ppath = IOPath.VToP(certPath);
            string key   = "";

            if (!File.Exists(ppath))
            {
                key = "ae" + IOPath.GetRandomString(14, 3).ToLower();
                byte[] text = Encoding.UTF8.GetBytes(key);
                File.WriteAllBytes(ppath, text);
            }
            else
            {
                key = ReadFileStr(certPath);
                if (string.IsNullOrEmpty(key))
                {
                    key = "ae" + IOPath.GetRandomString(14, 3).ToLower();
                    byte[] text = Encoding.UTF8.GetBytes(key);
                    File.WriteAllBytes(ppath, text);
                }
            }
            certKey = key;
            return(key);
        }
예제 #12
0
파일: SafeC.cs 프로젝트: baixue001/IPS
 /// <summary>
 /// 是否为图片文件
 /// </summary>
 public static bool IsImageFile(string fname)
 {
     return(IOPath.IsImageFile(fname));
 }