从流写入字符串

时间:2013-06-08 19:16:12

标签: c# readline console.writeline

我正在尝试使用streamwriter写入临时创建的文件,即_logFileName,同时使用流阅读器将写入文件的数据写入字符串。当前代码显示没有错误,但在运行时表示它无法从_logFileName中读取,因为它已在使用中。

我该怎么做?

        using (StreamWriter _logFile = File.CreateText(_logFileName))
        {
            //string s = "";
            //using (StreamReader fill_log = new StreamReader(s))
            using (StreamReader fill_log = new StreamReader(_logFileName)) 
            {
                _logFile.WriteLine("Logfile name is: " + _logFileName);
                content += fill_log.ReadLine(); 
                _logFile.WriteLine("LOG FILE STARTED AT: " + _startDateTime.ToString());
                content += fill_log.ReadLine(); 
                _logFile.WriteLine("============================================");
                content += fill_log.ReadLine(); 
                _logFile.Write(_message);
                content += fill_log.ReadLine(); 
                _logFile.WriteLine();
                content += fill_log.ReadLine(); 
            }
            _logFile.Close();
        }

因此根据建议我将代码更改为:

 using (var fsWrite = new FileStream(_logFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
        using (var _logFile = new StreamWriter(fsWrite))
        using (var fsRead = new FileStream(_logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (var fill_log = new StreamReader(fsRead))
        {
            _logFile.WriteLine();
            content += fill_log.ReadLine();  
            _logFile.WriteLine("TIME OF LOG ENTRY: " + DateTime.Now);
            content += fill_log.ReadLine();
            // Arbitrary objects can also be written to the file.
            _logFile.WriteLine(_message);
            content += fill_log.ReadLine();
            _logFile.Flush();
            _logFile.Close();

这样做,我能够同时红色并写入!没问题。谢谢。但内容字符串变量似乎在每个人之后结束。并想出为什么会发生这种情况?

2 个答案:

答案 0 :(得分:4)

为了能够同时从同一个文件读取和写入,您必须使用其中一个带有FileStream参数的构造函数手动创建FileShare对象,例如{{3} }。

using (var fsWrite = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
using (var _logFile = new StreamWriter(fsWrite))
using (var fsRead = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var fill_log = new StreamReader(fsRead))
{
    ...
}

答案 1 :(得分:1)

实现所需目标的另一种方法是使用编写StreamWriter和StringBuilder的专用TextWriter:

    using (StreamWriter _logFile = File.CreateText(_logFileName))
    {
        using (var builder = new StringBuildingStreamWriter(_logFile)) 
        {
            builder.WriteLine("Logfile name is: " + _logFileName);
            builder.WriteLine("LOG FILE STARTED AT: " + _startDateTime.ToString());
            builder.WriteLine("============================================");
            builder.Write(_message);
            builder.WriteLine();

            content += builder.ToString();
        }            
        _logFile.Close();
    }


public class StringBuildingStreamWriter:TextWriter
{
    StringBuilder sb = new StringBuilder();
    private StreamWriter sw;
    public StringBuildingStreamWriter(StreamWriter sw)
    {
        this.sw = sw;
    }

    public override void WriteLine(string value)
    {
        sb.AppendLine(value);
        sw.WriteLine(value);
    }

    public override void WriteLine()
    {
        sw.WriteLine();
        sb.AppendLine();
    }

    public override void Write(string value)
    {
        sb.Append(value);
        sw.Write(value);
    }

    public override string ToString()
    {
        return sb.ToString();
    }

    public override Encoding Encoding
    {
        get { return UTF8Encoding.UTF8; }
    }
}