C#读取锁定的文件
C# read locked file
到目前为止我已经尝试了所有方法都没有成功。
我想完成的是,我想打开文件并将其锁定一段时间。
在我打开并锁定文件后,我尝试打开同一个文件只是为了阅读目的。
string filePath = "test.ini";
// Open and lock the file
FileStream configurationFile = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
configurationFile.Lock(1, configurationFile.Length);
// Open the same file just to read it
using (FileStream bufferStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(bufferStream))
{
string line;
while ((line = sr.ReadLine()) != null) // <--- Here I get the error
{
CustomMessageBox.Show(line);
}
}
}
我实际上可以使用 FileStream 和 StreamReader 打开文件,但是当使用 StreamReader 时 sr.ReadLine() 它会抛出一个异常,文件正在被另一个进程使用。
如此处所述Reading a file used by another process [duplicate] FileShare 属性应该是 ReadWrite 但这没有帮助。
此外,我已经尝试了所有可用的编码,例如 StreamReader(bufferStream, Encoding.*)
,但也没有用。
我忽略了一些简单的事情吗?
您正在对文件流调用 Lock,并传递参数以表明您希望锁定整个文件。根据文档,
Locking a range of a file stream gives the threads of the locking process exclusive access to that range of the file stream.
如果您不想锁定文件,请不要调用 Lock
。
那是因为根据 Windows,您正在构建一个新的文件句柄。
当文件被锁定时,它不允许任何其他句柄被其他流获取。你最好像这样修复你的代码:
using (FileStream bufferStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
bufferStream.Lock(0L, bufferStream.Length);
using (StreamReader sr = new StreamReader(bufferStream))
{
string line;
while ((line = sr.ReadLine()) != null) // <--- Here I get the error
{
CustomMessageBox.Show(line);
}
}
// dispose lock
bufferStream.Unlock(0L, bufferStream.Length);
}
到目前为止我已经尝试了所有方法都没有成功。
我想完成的是,我想打开文件并将其锁定一段时间。 在我打开并锁定文件后,我尝试打开同一个文件只是为了阅读目的。
string filePath = "test.ini";
// Open and lock the file
FileStream configurationFile = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
configurationFile.Lock(1, configurationFile.Length);
// Open the same file just to read it
using (FileStream bufferStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(bufferStream))
{
string line;
while ((line = sr.ReadLine()) != null) // <--- Here I get the error
{
CustomMessageBox.Show(line);
}
}
}
我实际上可以使用 FileStream 和 StreamReader 打开文件,但是当使用 StreamReader 时 sr.ReadLine() 它会抛出一个异常,文件正在被另一个进程使用。
如此处所述Reading a file used by another process [duplicate] FileShare 属性应该是 ReadWrite 但这没有帮助。
此外,我已经尝试了所有可用的编码,例如 StreamReader(bufferStream, Encoding.*)
,但也没有用。
我忽略了一些简单的事情吗?
您正在对文件流调用 Lock,并传递参数以表明您希望锁定整个文件。根据文档,
Locking a range of a file stream gives the threads of the locking process exclusive access to that range of the file stream.
如果您不想锁定文件,请不要调用 Lock
。
那是因为根据 Windows,您正在构建一个新的文件句柄。 当文件被锁定时,它不允许任何其他句柄被其他流获取。你最好像这样修复你的代码:
using (FileStream bufferStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
bufferStream.Lock(0L, bufferStream.Length);
using (StreamReader sr = new StreamReader(bufferStream))
{
string line;
while ((line = sr.ReadLine()) != null) // <--- Here I get the error
{
CustomMessageBox.Show(line);
}
}
// dispose lock
bufferStream.Unlock(0L, bufferStream.Length);
}