using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using JWS.Com.Model;
namespace JWS.CWS.FW.App
{
<summary>
</summary>
public class KeyValue
{
<summary>
</summary>
<param name="key"></param>
<param name="value"></param>
public KeyValue(string key, string value)
{
this._key = key;
this._value = value;
}
<summary>
</summary>
private string _key;
<summary>
</summary>
<returns></returns>
public string Getkey()
{
return _key;
}
<summary>
</summary>
private string _value;
<summary>
</summary>
<returns></returns>
public string GetValue()
{
return _value;
}
<summary>
</summary>
<returns></returns>
public override string ToString()
{
return string.Format("key = {0}, value = {1}", _key, _value);
}
}
<summary>
</summary>
public class EnvironmentFile
{
<summary>
</summary>
private string _EnvFile = ComConst.stEnvDir + "\\COMENVFILE.ini";
<summary>
</summary>
private DateTime _LastUpdateTime;
<summary>
</summary>
private ReaderWriterLock rwLock = new ReaderWriterLock();
<summary>
</summary>
private static EnvironmentFile _Instance;
<summary>
</summary>
<returns></returns>
public static EnvironmentFile Instance
{
get
{
if (EnvironmentFile._Instance == null)
{
EnvironmentFile instance = new EnvironmentFile();
instance.Init();
EnvironmentFile._Instance = instance;
}
return EnvironmentFile._Instance;
}
}
<summary>
</summary>
public void Init()
{
try
{
IsolatedStorageFile storageFile = JWS.CWS.Storage.IsolatedStorage.GetUserStore();
if (!storageFile.FileExists(_EnvFile))
{
throw new IOException();
}
this._LastUpdateTime = storageFile.GetLastWriteTime(_EnvFile).DateTime;
Load();
}
finally
{
}
}
<summary>
</summary>
public void Clear()
{
try
{
rwLock.AcquireWriterLock(Timeout.Infinite);
if (_Items != null)
{
_Items.Clear();
}
else
{
_Items = new Dictionary<string, Dictionary<string, string>>();
}
}
finally
{
rwLock.ReleaseWriterLock();
}
}
<summary>
</summary>
<returns></returns>
protected bool IsUpdate()
{
IsolatedStorageFile storageFile = JWS.CWS.Storage.IsolatedStorage.GetUserStore();
if (!storageFile.FileExists(_EnvFile))
{
return false;
}
DateTime lastWriteTime = storageFile.GetLastWriteTime(_EnvFile).DateTime;
return (this._LastUpdateTime < lastWriteTime);
}
<summary>
</summary>
protected Dictionary<string, Dictionary<string, string>> _Items;
<summary>
</summary>
protected void Load()
{
try
{
Clear();
rwLock.AcquireWriterLock(Timeout.Infinite);
using (IsolatedStorageFile file = JWS.CWS.Storage.IsolatedStorage.GetUserStore())
{
using (IsolatedStorageFileStream stream
= new IsolatedStorageFileStream(_EnvFile, FileMode.Open, file))
{
using (StreamReader reader = new StreamReader(stream))
{
string stLine;
string line;
string section = null;
string currentSection = null;
KeyValue keyValue;
while ((stLine = reader.ReadLine()) != null)
{
line = stLine.Trim();
if (line.Length == 0)
{
continue;
}
if (IsComment(line))
{
continue;
}
section = ParseSection(line);
if (section != null)
{
currentSection = section;
continue;
}
keyValue = ParseKeyValue(line);
if (keyValue != null)
{
AddItem(currentSection, keyValue);
}
}
}
}
this._LastUpdateTime = file.GetLastWriteTime(_EnvFile).DateTime;
}
}
finally
{
rwLock.ReleaseWriterLock();
}
}
<summary>
</summary>
<param name="line"></param>
<returns></returns>
protected bool IsComment(string line)
{
bool result = false;
if (line.StartsWith("#") || line.StartsWith("\\"))
{
result = true;
}
return result;
}
<summary>
</summary>
<param name="line"></param>
<returns></returns>
protected string ParseSection(string line)
{
string section = null;
if (line.Length > 2 && line.StartsWith("[") && line.EndsWith("]"))
{
section = line.Substring(1, line.Length - 2);
}
return section;
}
<summary>
</summary>
<param name="line"></param>
<returns></returns>
protected KeyValue ParseKeyValue(string line)
{
int index = line.IndexOf("=");
KeyValue result = null;
if (index >= 0)
{
string key = line.Substring(0, index);
string value = line.Substring(index + 1);
if (key != null)
{
result = new KeyValue(key, value);
}
}
return result;
}
<summary>
</summary>
<param name="section"></param>
<param name="keyvalue"></param>
protected void AddItem(string section, KeyValue item)
{
try
{
rwLock.AcquireWriterLock(Timeout.Infinite);
if (section == null)
{
section = "";
}
Dictionary<string, string> keyValues = null;
if (!this._Items.TryGetValue(section, out keyValues))
{
keyValues = new Dictionary<string, string>();
keyValues.Add(item.Getkey().Trim(), item.GetValue().Trim());
this._Items.Add(section, keyValues);
}
keyValues[item.Getkey().Trim()] = item.GetValue().Trim();
}
finally
{
rwLock.ReleaseWriterLock();
}
}
<summary>
</summary>
protected void ConfirmUpdate()
{
if (IsUpdate())
{
Load();
}
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public string GetString(string section, string key, string defaultValue)
{
try
{
ConfirmUpdate();
rwLock.AcquireReaderLock(Timeout.Infinite);
string value = defaultValue;
do
{
if (_Items == null)
{
break;
}
Dictionary<string, string> keyValues = null;
if (!this._Items.TryGetValue(section, out keyValues))
{
break;
}
if (keyValues.ContainsKey(key))
{
value = keyValues[key];
}
} while (false);
return value;
}
finally
{
rwLock.ReleaseReaderLock();
}
}
public string GetString(string section, string key)
{
return GetString(section, key, null);
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public Nullable<int> GetInt(string section, string key, Nullable<int> defaultValue)
{
try
{
Nullable<int> value = null;
string valueString = GetString(section, key, null);
if (valueString == null)
{
value = defaultValue;
}
else
{
value = int.Parse(valueString);
}
return value;
}
catch (FormatException)
{
return defaultValue;
}
finally
{
}
}
public Nullable<int> GetInt(string section, string key)
{
return GetInt(section, key, null);
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public Nullable<bool> GetBoolean(string section, string key, Nullable<bool> defaultValue)
{
try
{
Nullable<bool> value = null;
string valueString = GetString(section, key, null);
if (valueString == null)
{
return defaultValue;
}
valueString = valueString.ToLower();
if ("true".Equals(valueString))
{
value = true;
}
else if ("false".Equals(valueString) || "0".Equals(valueString))
{
value = false;
}
else
{
if (int.Parse(valueString) > 0)
{
value = true;
}
else
{
value = defaultValue;
}
}
return value;
}
catch (FormatException)
{
return defaultValue;
}
finally
{
}
}
public Nullable<bool> GetBoolean(string section, string key)
{
return GetBoolean(section, key, null);
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public Nullable<long> GetLong(string section, string key, Nullable<long> defaultValue)
{
try
{
Nullable<long> value = null;
string valueString = GetString(section, key, null);
if (valueString == null)
{
value = defaultValue;
}
else
{
value = long.Parse(valueString);
}
return value;
}
catch (FormatException)
{
return defaultValue;
}
finally
{
}
}
public Nullable<long> GetLong(string section, string key)
{
return GetLong(section, key, null);
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public Nullable<float> GetFloat(string section, string key, Nullable<float> defaultValue)
{
try
{
Nullable<float> value = null;
String valueString = GetString(section, key, null);
if (valueString == null)
{
value = defaultValue;
}
else
{
value = float.Parse(valueString);
}
return value;
}
catch (FormatException)
{
return defaultValue;
}
finally
{
}
}
public Nullable<float> GetFloat(string section, string key)
{
return GetFloat(section, key, null);
}
<summary>
</summary>
<param name="section"></param>
<param name="key"></param>
<param name="defaultValue"></param>
<returns></returns>
public Nullable<double> GetDouble(string section, string key, Nullable<double> defaultValue)
{
try
{
Nullable<double> value = null;
string valueString = GetString(section, key, null);
if (valueString == null)
{
value = defaultValue;
}
else
{
value = double.Parse(valueString);
}
return value;
}
catch (FormatException)
{
return defaultValue;
}
finally
{
}
}
public Nullable<double> GetDouble(string section, string key)
{
return GetDouble(section, key, null);
}
}
}