Отправлено 27 February 2010 - 09:07
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.SmallBasic.Library;
namespace MyExtensions
{
/// <summary>
/// The Settings library consists of helpers that allow programs to
/// store and retrieve user settings.
/// </summary>
[SmallBasicType]
public static class Settings
{
static Primitive _filePath = new Primitive();
/// <summary>
/// Gets the file path for the settings file.
/// </summary>
public static Primitive FilePath
{
get
{
if (string.IsNullOrEmpty(_filePath))
{
_filePath = Path.ChangeExtension(
Assembly.GetEntryAssembly().Location,
".settings");
}
return _filePath;
}
}
/// <summary>
/// Gets the value for the setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <returns>
/// The Value of the setting.
/// </returns>
public static Primitive GetValue(Primitive name)
{
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
Dictionary<string, string> contents = ReadContents(stream);
if (contents.ContainsKey (name)) { return contents[name]; }
}
}
return "";
}
/// <summary>
/// Sets a value for a setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <param name="value">
/// The Value of the setting.
/// </param>
public static void SetValue(Primitive name, Primitive value)
{
Dictionary<string, string> contents = null;
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
contents = ReadContents(stream);
}
}
else
{
contents = new Dictionary<string, string>();
}
contents[name] = value;
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Create))
{
WriteContents(stream, contents);
}
}
static Dictionary<string, string> ReadContents(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
return (Dictionary<string, string>)formatter.Deserialize(stream);
}
static void WriteContents(Stream stream, Dictionary<string, string> map)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.Serialize(stream, map);
}
}
}
Это код расширения я хз че сним делать он позволяет хранить и вочсстанавливать пары имя и значение