【发布时间】:2014-09-15 09:45:05
【问题描述】:
我正在尝试将数据保存到 csv 文件,但想确保我要保存的数据尚未在 csv 文件中存在。因此,我想先读取文件,然后保存数据。
FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamWriter sw = new StreamWriter(writer))
{
if (response.Datapoints.Count != 0)
{
foreach (var datapoint in response.Datapoints)
{
sb = new StringBuilder();
toBeSaved = new string[] { nameSpace, metric, instanceId, datapoint.Timestamp.ToString()};
foreach (string str in toBeSaved)
{
if (str == toBeSaved.Last())
{
sb.Append(str);
}
else
{
sb.Append(str);
sb.Append(",");
}
}
// here I get the error that the "Stream is not Readable"
using (StreamReader sr = new StreamReader(reader))
{
if (sr.ReadLine() != sb.ToString())
{ // only write the data if it is not already there in the file
sw.WriteLine(sb.ToString());
}
}
}
【问题讨论】:
标签: c# csv streamreader streamwriter