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;
    }
}

 

转载于:https://www.cnblogs.com/kellynic/archive/2011/12/05/2276576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值