本文介绍了阅读在C#中打开的文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
喜我有一个记录一些数据,在具体路径文本文件的程序。(log.txt中)我可以用记事本打开该文件(log.txt中)和读它是什么。
hiI have a program that log some data in text file in specific path.(log.txt)I can open the file (log.txt) with notepad and read what is in it.
现在我正在写一个程序来读取log.txt的,但我得到的异常因为它正由另一个进程使用进程无法访问文件log.txt中'。
now I'm writing a program to read log.txt but I get the exception"The process cannot access the file 'log.txt' because it is being used by another process."
我该怎么办?
推荐答案
试试这个:
using (var stream = File.Open("log.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(stream))
{
// Actions you perform on the reader.
}
您是否可以打开该文件依赖于文件共享
您已经打开日志文件时提供的。在上面的例子中的设置是相当低的,也许有助于打开文件
Whether you can open the file depends on the FileShare
you've provided when opening the log file. The settings in the example above are quite low and maybe helps opening the file.
这篇关于阅读在C#中打开的文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!