我想阅读一个不断变化的文本文件。
但是第一个问题是文件太大,第一次挂了,而且这个文本文件(txt)的每一秒都在变化。
是不是第一次只调用了文件的最后50行?所以程序没有停止
而且它更容易阅读和不断变化的添加......
我想阅读一个不断变化的文本文件。
但是第一个问题是文件太大,第一次挂了,而且这个文本文件(txt)的每一秒都在变化。
是不是第一次只调用了文件的最后50行?所以程序没有停止
而且它更容易阅读和不断变化的添加......
观看您感兴趣的文件。
static class Program
{
static long position = 0;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = System.Environment.CurrentDirectory;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "data.txt"; // or *.txt for all .txt files.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
using (FileStream fileStream = new FileStream("data.txt", FileMode.Open))
{
// Using Ron Deijkers answer, skip to the part you din't read.
fileStream.Seek(position, SeekOrigin.End);
for (int i = 0; i < fileStream.Length; i++)
{
fileStream.ReadByte();
}
}
}
}
假设我理解正确,我认为您应该不时重新打开文件以从中读取,然后使用 FileStream 的 Seek 方法。
请参阅:http: //msdn.microsoft.com/en-us/library/system.io.filestream.seek.aspx
每次从文件中读取时,您应该将位置存储到您读取文件的位置。当您开始读取另一个块时,您可以使用该偏移量和 seek 方法转到您尚未读取的文件部分。
这样,您可以分块读取文件,而不会将其锁定太久(从而阻止对其的写入操作)
然后线程(或 Timer 对象)可以不时地从文件中读取。确保块不要太大,以免文件锁定太久。
这可能无法准确展示您需要的程序流程,但它确实为您提供了不会挂起您的 UI(异步)的阅读和写作。希望你能够适应你需要的东西。
public class AsyncFileUpdate
{
object locker = new object();
public FileInfo File { get; private set; }
public AsyncFileUpdate(FileInfo file)
{
File = file;
}
/// <summary>
/// Reads contents of a file asynchronously.
/// </summary>
/// <returns>A task representing the asynchronous operation</returns>
public Task<string> ReadFileAsync()
{
return Task.Factory.StartNew<string>(() =>
{
lock (locker)
{
using (var fs = File.OpenRead())
{
StreamReader reader = new StreamReader(fs);
using (reader)
{
return reader.ReadToEnd();
}
}
}
});
}
/// <summary>
/// write file asynchronously
/// </summary>
/// <param name="content">string to write</param>
/// <returns>A task representing the asynchronous operation</returns>
public Task WriteFileAsync(string content)
{
return Task.Factory.StartNew(() =>
{
lock (locker)
{
using (var fs = File.OpenWrite())
{
StreamWriter writer = new StreamWriter(fs);
using (writer)
{
writer.Write(content);
writer.Flush();
}
}
}
});
}
}
/// <summary>
/// Demonstrates usage
/// </summary>
public class FileOperations
{
public void ProcessAndUpdateFile(FileInfo file)
{
AsyncFileUpdate fu = new AsyncFileUpdate(file); ;
fu.ReadFileAsync()
.ContinueWith(p => Process(p.Result))
.ContinueWith(p => fu.WriteFileAsync(p.Result));
}
/// <summary>
/// does the processing on the file content
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
string Process(string content)
{
throw new NotImplementedException("you do this bit ;)");
}
}
所有这些Task
业务都来自Task Parallel Library - 一个出色的工具包,用于消除并行和异步编程的麻烦。http://msdn.microsoft.com/en-us/library/dd537608.aspx
注意:文件系统访问是相当昂贵的并且物理降级到存储介质。你在控制这个文件(你创建它)吗?每秒更新一个文件是非常令人望而却步的。如果您在检查文件时担心文件更改,也许您需要先制作一份副本?
对于文本文件的异步读取,请尝试使用FileHelpers库