📄 filestore.cs
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Microsoft.Mobile.Data
{
/// <summary>
/// Implementation of the <see cref="Microsoft.Mobile.Data.DataStore"/> to read
/// a file as collection of rows and columns.
/// </summary>
public class FileStore : DataStore
{
#region Fields
string _fileName;
#endregion
#region Constructor(s) & Dispose
/// <summary>
/// Store the connection string. The file is not openned at this time.
/// </summary>
/// <param name="connectionString">The name of the file</param>
public FileStore(string connectionString)
: base(connectionString)
{
_fileName = connectionString;
}
/// <summary>
/// Optional DataStore construction signature which allows for
/// implementation specific parameters to be passed to a specific
/// implementation.
/// </summary>
/// <param name="connectionString">
/// string containing connection information
/// </param>
/// <param name="implementationParameters">
/// implementation specific additional parameters
/// </param>
protected FileStore(string connectionString, Parameters implementationParameters)
: base(connectionString, implementationParameters)
{
}
/// <summary>
/// Needs to be implemented to since it is abstract
/// </summary>
protected override void Dispose(bool disposing)
{
}
#endregion
#region Methods
/// <summary>
/// Files are not transactional so BeginTransaction not implemented.
/// </summary>
public override void BeginTransaction()
{
throw new NotImplementedException();
}
/// <summary>
/// Files are not held open so Close not implemented.
/// </summary>
public override void Close()
{
throw new NotImplementedException();
}
/// <summary>
/// Files are not transactional so Commit not implemented.
/// </summary>
public override void Commit()
{
throw new NotImplementedException();
}
/// <summary>
/// Opens the file and wraps it in an enumerator.
/// </summary>
/// <param name="selector">not used currently</param>
/// <returns>enumerator to read each line of the file</returns>
public override RowEnumerator Read(string selector)
{
return new EnumerateFile(_fileName);
}
/// <summary>
/// Opens the file and wraps it in an enumerator.
/// </summary>
/// <param name="selector">not used currently</param>
/// <param name="parameters">not used</param>
/// <returns>enumerator to read each line of the file</returns>
public override RowEnumerator Read(string selector,
Parameters parameters)
{
return Read(selector);
}
/// <summary>
/// Reads the first line of the file.
/// </summary>
/// <param name="selector">not used currently</param>
/// <returns>contents of the first line of the file</returns>
public override object GetValue(string selector)
{
RowEnumerator row;
string result = string.Empty;
row = Read(null);
if (row.MoveNext())
{
result = row.Current["FileValue"].ToString();
}
return result;
}
/// <summary>
/// Reads the first line of the file.
/// </summary>
/// <param name="selector">not used currently</param>
/// <param name="parameters">not used</param>
/// <returns>contents of the first line of the file</returns>
public override object GetValue(string selector, Parameters parameters)
{
return GetValue(null);
}
/// <summary>
/// Files are not transactional so Rollback not implemented.
/// </summary>
public override void Rollback()
{
throw new NotImplementedException();
}
/// <summary>
/// Writes a line to the end of a file.
/// </summary>
/// <param name="update">contents to write to a file</param>
public override void Save(string update)
{
Save(update, null);
}
/// <summary>
/// Writes a line to the end of a file.
/// </summary>
/// <param name="update">contents to write to a file</param>
/// <param name="parameters">not used</param>
public override void Save(string update, Parameters parameters)
{
StringBuilder writeLine;
writeLine = new StringBuilder(update);
foreach (KeyValuePair<string, object> currentParameter in parameters)
{
writeLine.Replace(currentParameter.Key, currentParameter.Value.ToString());
}
(new StreamWriter(_fileName, true)).WriteLine(writeLine);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -