英文:
C# .net. Having trouble catching a file as read-only when I have it open
问题
我有一个方法,它只用于打开一个.txt文件以进行读取。我已经让它运行得很好,但现在我正在尝试确保如果有其他人打开了该文件,我不会访问它。我尝试过其他解决方案,但仍然遇到问题。
public string Load()
{
string source = MessagesAndNotifications.SourceDrawingNotSet;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "afile (*.txt)|*.txt";
Nullable<bool> result = openFileDialog.ShowDialog();
FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
if (fileInfo.IsReadOnly == false)
{
if (result == true)
{
try
{
using (var fileStream = File.Open(openFileDialog.FileName, FileMode.Open,
FileAccess.Read, FileShare.None))
{
using (var streamReader = new StreamReader(fileStream.Name))
{
// 其余代码继续执行...
}
}
}
// 异常处理...
}
}
}
问题是我的IsReadOnly检查返回false,即使我自己已经打开了文件。然后,当我的程序到达try语句时,它会捕获文件已被打开,并且程序会抛出异常并崩溃。
我最初尝试使用File.OpenRead()
方法而不是带参数的File.Open()
,结果相同。
英文:
I have a method that is opening a .txt file for reading only. I have it working well but now I am trying to ensure that I don't access the file if someone else has it open. I have tried other solutions but I'm still having issues.
public string Load()
{
string source = MessagesAndNotifications.SourceDrawingNotSet;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "afile (*.txt)|*.txt";
Nullable<bool> result = openFileDialog.ShowDialog();
FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
if (fileInfo.IsReadOnly == false)
{
if (result == true)
{
try
{
using (var fileStream = File.Open(openFileDialog.FileName, FileMode.Open,
FileAccess.Read, FileShare.None))
{
using (var streamReader = new StreamReader(fileStream.Name))
{
//rest of the code goes on...
}
The problem is my check on IsReadOnly is returning false even though I have opened the file myself. Then when my program gets to the try statement it catches the file being open and the program throws an exception and crashes.
I had originally tried having the File.OpenRead()
method instead of File.Open()
with parameters and got the same result.
答案1
得分: 1
“IsReadonly”属性与文件系统的打开模式无关。
如果文件是只读的,您只能读取它。
但如果文件不是只读的,您仍然可以以只读模式打开它,并且只能读取它。
英文:
The "IsReadonly"-Attribute on the FileSystem has nothing to do with the opening-Mode.
If a file is readonly, you can only read it.
But if a file is not readonly, you still can open it for reading, and read it only.
答案2
得分: 1
FileInfo.IsReadOnly仅检查文件属性“只读”是否为true,而不检查文件是否正在使用。至少根据我所了解的情况是这样。
https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.isreadonly?view=netframework-4.8
我唯一能想到这种方法有效的方式是,每当有人打开文件时,将“只读”标志添加到文件中。
我建议您查找“正在使用”的解决方案:
https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
英文:
FileInfo.IsReadOnly only checks if the file attribute "read-only" is true, not if the file is in use. At least, to the best of my understanding.
https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.isreadonly?view=netframework-4.8
The only way I could see that approach working is if the "read-only" flag is added to the file whenever somebody opens it.
I would instead suggest researching "in use" solutions:
https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论