0

我正在尝试使用流写入器写入临时创建的文件,即_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();

这样做,我可以同时红和写!这没有问题。谢谢。但是内容字符串变量似乎在everyright之后结束。和想法为什么会发生这种情况?

4

2 回答 2

4

为了能够同时读取和写入同一个文件,您必须使用采用FileShare参数的构造函数之一手动创建FileStream对象,例如this one

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))
{
    ...
}
于 2013-06-08T19:30:51.953 回答
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; }
    }
}
于 2013-06-08T20:04:24.140 回答