zoukankan      html  css  js  c++  java
  • IniHelper.cs

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.IO;
    using System.Web;

    public class IniHelper {
        private static Dictionary<stringobject> _cache = new Dictionary<stringobject>();
        private static Dictionary<string, FileSystemWatcher> _watcher = new Dictionary<string, FileSystemWatcher>();
        private static object _lock = new object();

        private static object loadAndCache(string path) {
            path = TranslateUrl(path);
            object ret = null;
            if (!_cache.TryGetValue(path, out ret)) {
                object value2 = LoadIniNotCache(path);
                string dir = Path.GetDirectoryName(path);
                string name = Path.GetFileName(path);
                FileSystemWatcher fsw = new FileSystemWatcher(dir, name);
                fsw.IncludeSubdirectories = false;
                fsw.Changed += watcher_handler;
                fsw.Renamed += watcher_handler;
                fsw.EnableRaisingEvents = false;
                lock (_lock) {
                    if (!_cache.TryGetValue(path, out ret)) {
                        _cache.Add(path, ret = value2);
                        _watcher.Add(path, fsw);
                        fsw.EnableRaisingEvents = true;
                    } else {
                        fsw.Dispose();
                    }
                }
            }
            return ret;
        }
        private static void watcher_handler(object sender, FileSystemEventArgs e) {
            lock (_lock) {
                _cache.Remove(e.FullPath);
                FileSystemWatcher fsw = null;
                if (_watcher.TryGetValue(e.FullPath, out fsw)) {
                    fsw.EnableRaisingEvents = false;
                    fsw.Dispose();
                }
            }
        }

        public static Dictionary<string, NameValueCollection> LoadIni(string path) {
            return loadAndCache(path) as Dictionary<string, NameValueCollection>;
        }
        public static Dictionary<string, NameValueCollection> LoadIniNotCache(string path) {
            Dictionary<string, NameValueCollection> ret = new Dictionary<string, NameValueCollection>();
            string[] lines = ReadTextFile(path).Split(new string[] { "\n" }, StringSplitOptions.None);
            string key = "";
            foreach (string line2 in lines) {
                string line = line2.Trim();
                int idx = line.IndexOf('#');
                if (idx != -1) line = line.Remove(idx);
                if (string.IsNullOrEmpty(line)) continue;

                Match m = Regex.Match(line, @"^\[([^\]]+)\]$");
                if (m.Success) {
                    key = m.Groups[1].Value;
                    continue;
                }
                if (!ret.ContainsKey(key)) ret.Add(key, new NameValueCollection());
                string[] kv = line.Split(new char[] { '=' }, 2);
                if (!string.IsNullOrEmpty(kv[0])) {
                    ret[key][kv[0]] = kv.Length > 1 ? kv[1] : null;
                }
            }
            return ret;
        }

        public static string ReadTextFile(string path) {
            byte[] bytes = ReadFile(path);
            return Encoding.UTF8.GetString(bytes).TrimStart((char)65279);
        }
        public static byte[] ReadFile(string path) {
            path = TranslateUrl(path);

            if (File.Exists(path)) {
                //string destFileName = Path.GetTempFileName();
                
    //File.Copy(path, destFileName, true);
                string destFileName = path;
                int read = 0;
                byte[] data = new byte[1024 * 8];
                MemoryStream ms = new MemoryStream();
                using (FileStream fs = new FileStream(destFileName, FileMode.OpenOrCreate, FileAccess.Read)) {
                    do {
                        read = fs.Read(data, 0, data.Length);
                        if (read <= 0break;
                        ms.Write(data, 0, read);
                    } while (true);
                    fs.Close();
                }
                //File.Delete(destFileName);
                data = ms.ToArray();
                ms.Close();
                return data;
            }
            return new byte[] { };
        }

        public static string TranslateUrl(string url) {
            return TranslateUrl(url, null);
        }
        private static object _ecd_lock = new object();
        public static string TranslateUrl(string url, string baseDir) {
            if (string.IsNullOrEmpty(url)) url = "";

            if (!string.IsNullOrEmpty(baseDir) && !url.StartsWith("~/") && !url.StartsWith("/")) {
                baseDir = Path.GetDirectoryName(baseDir);
                if (HttpContext.Current != null)
                    url = Path.GetFullPath(baseDir + "\\" + url);
                else {
                    lock (_ecd_lock) {
                        string ecd = Environment.CurrentDirectory;
                        Environment.CurrentDirectory = baseDir;
                        url = Path.GetFullPath(url);
                        Environment.CurrentDirectory = ecd;
                    }
                }
            } else {
                if (url.IndexOf(":\\") == -1) {
                    if (System.Web.HttpContext.Current != null)
                        url = System.Web.HttpContext.Current.Server.MapPath(url);
                    else
                        url = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "\\" + url.Trim('\\''~'));
                }
            }
            return url;
        }
    }
  • 相关阅读:
    nvm安装node的问题
    前端必读:浏览器内部工作原理
    读书笔记:《高性能网站建设指南》
    学习前端我读过的书
    Canvas绘制圆形进度条
    gitlab升级方法
    设置root远程连接ubuntu上mysql
    SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
    JSTL 格式化输出 Calendar
    在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置
  • 原文地址:https://www.cnblogs.com/kellynic/p/2276576.html
Copyright © 2011-2022 走看看