VB.NET 在继续读/写之前检查文件是否打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11287502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Checking if a File is Open before proceeding with a Read/Write?
提问by Dayan
Is there a method to verify that a file is open? The only thing I can think of is the Try/Catch
to see if i can catch the file-open exception but I figured that a method be available to return true/false if file is open.
有没有办法验证文件是否打开?我唯一能想到的是Try/Catch
看看我是否可以捕获文件打开异常,但我认为如果文件打开,可以使用一种方法来返回真/假。
Currently using System.IO
and the following code under class named Wallet
.
当前使用System.IO
和下面的代码在名为Wallet
.
Private holdPath As String = "defaultLog.txt"
Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Private file As New StreamWriter(_file)
Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal
Try
file.WriteLine("testing")
file.Close()
Catch e As IOException
'Note sure if this is the proper way.
End Try
Return 0D
End Function
Any pointers will be appreciated! Thank you!!
任何指针将不胜感激!谢谢!!
回答by Jeremy Thompson
Private Sub IsFileOpen(ByVal file As FileInfo)
Dim stream As FileStream = Nothing
Try
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
stream.Close()
Catch ex As Exception
If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
' do something here, either close the file if you have a handle, show a msgbox, retry or as a last resort terminate the process - which could cause corruption and lose data
End If
End Try
End Sub
Private Shared Function IsFileLocked(exception As Exception) As Boolean
Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
Return errorCode = 32 OrElse errorCode = 33
End Function
回答by jussij
There is really no point using a 'is file in use check' function since you will still need to have try catch to handle the case that the file fails to open. The file open can fail for many more reasons than it just being already open.
使用“正在使用的文件检查”功能确实没有意义,因为您仍然需要 try catch 来处理文件无法打开的情况。文件打开失败的原因不仅仅是它已经打开了。
Also using a function to do a check is no guarantee of success. The 'is file in use check' might return false only for the file open to fail with a file already open error, because in time between the check and trying to open the file it was opened by someone else.
同样使用函数进行检查并不能保证成功。'is file in use check' 可能仅在文件打开失败并出现文件已打开错误时返回 false,因为在检查和尝试打开文件之间的时间已被其他人打开。
回答by Mark Hall
It looks like the two suggestions from this MSDN forum postingboth involve trying to open the file.
看起来来自这个MSDN 论坛帖子的两个建议都涉及尝试打开文件。
The first one is similar to what you are doing now, and the second involves using a Windows API function (CreateFile) and checking for a invalid handle signifying the file is in use. In both cases they are relying on an error condition to determine if the file is open or not. In short, in my opinion the method you are using is correct since there is nota System.IO.File.IsOpen property.
第一个与您现在正在做的类似,第二个涉及使用 Windows API 函数 (CreateFile) 并检查表示文件正在使用的无效句柄。在这两种情况下,他们都依赖错误条件来确定文件是否打开。简而言之,我认为您使用的方法是正确的,因为没有System.IO.File.IsOpen 属性。