public class Manager : IDisposable { #region Constructors public Manager(string _File) { fs = new FileStream(_File, FileMode.OpenOrCreate); sw = new StreamWriter(fs); sr = new StreamReader(fs); sb = new StringBuilder(); } #endregion #region Private Fields private FileStream fs; private StreamWriter sw; private StreamReader sr; private StringBuilder sb; const char def = '*'; const int ByteSize = 4, ShortSize = 6, IntSize = 11, LongSize = 20, StringSize = 20; #endregion #region Methods public void Dispose() { sw.Dispose(); sw.Close(); } private void Write(string value, int MaxLength, int position) { lock (fs) { sb.Append(value); sb.Append(def, MaxLength - value.Length); fs.Seek(position, SeekOrigin.Begin); sw.Write(sb.ToString()); sb.Clear(); } } public void WriteString(string value, int position) { Write(value, StringSize, position); } public void WriteUlong(ulong val, int position) { Write(val.ToString(), LongSize, position); } public void WriteLong(long val, int position) { Write(val.ToString(), LongSize, position); } public void WriteUInt(uint val, int position) { Write(val.ToString(), IntSize, position); } public void WriteInt(int val, int position) { Write(val.ToString(), IntSize, position); } public void WriteUshort(ushort val, int position) { Write(val.ToString(), ShortSize, position); } public void WriteShort(short val, int position) { Write(val.ToString(), ShortSize, position); } public void WriteByte(byte val, int position) { Write(val.ToString(), ByteSize, position); } public void WriteBool(bool val, int position) { WriteByte((byte)(val == true ? 1 : 0), position); } public string GetString(int position, int length) { lock (fs) { fs.Seek(position, SeekOrigin.Begin); string val = sr.ReadToEnd(); return val.Substring(0, val.IndexOf(def)); //return val.TrimEnd(def); } } public string GetString(int position) { return GetString(position, StringSize); } public ulong GetUlong(int position) { return ulong.Parse(GetString(position, LongSize)); } public long GetLong(int position) { return long.Parse(GetString(position, LongSize)); } public uint GetUInt(int position) { return uint.Parse(GetString(position, IntSize).TrimEnd('*')); } public int GetInt(int position) { return int.Parse(GetString(position, IntSize)); } public ushort GetUshort(int position) { return ushort.Parse(GetString(position, ShortSize)); } public short GetShort(int position) { return short.Parse(GetString(position, ShortSize)); } public byte GetByte(int position) { return byte.Parse(GetString(position, ByteSize)); } public bool GetBool(int position) { return GetByte(position) == 1 ? true : false; } #endregion }
Bueno creo que es mejor si abres uno cada vez que se necesite y a realices todos lo que ocupes con el y luego lo cierres, el cerrado es muy importante por que si no se cierra podrias tener problemas de acceso hacie el filestream.
private void Write(string value, int MaxLength, int position) { lock (fs) { sb.Append(value); sb.Append(def, MaxLength - value.Length); fs.Seek(position, SeekOrigin.Begin); sw.Write(sb.ToString()); sb.Clear(); } }
private void Write(string value, int MaxLength, int position) { lock (fs) { sb.Append(value); sb.Append(def, MaxLength - value.Length); fs = new FileStream()//omitire los parametros fs.Seek(position, SeekOrigin.Begin); sw = new StreamWriter() sw.Write(sb.ToString()); sw.Close(); sb.Clear(); } }