简体   繁体   English

尝试删除文件后如何确认文件正在使用中?

[英]How to confirm a file is in use after it attempted to delete?

Is there a way to know a file is in use, after attempting it to delete? 尝试删除文件 ,是否有办法知道文件正在使用中?

The DeleteFile() doesn't return any value to verify such scenario. DeleteFile()不返回任何值来验证这种情况。 is there any alternative that can be used to validate "an error message is thrown", if I tried to delete a file in use? 如果我尝试删除正在使用的文件,是否还有其他方法可以用来验证“抛出错误消息”?

AFAIK, there is no explicit method in .NET. AFAIK,.NET中没有显式方法。

You can, however, open and close the file with exclusive permissions and catch the error: 但是,您可以使用独占权限打开和关闭文件并捕获错误:

Public Function IsFileInUse(ByVal myFile As String) As Boolean
    If File.Exists(myFile) Then
        Try
            Dim hnd As Short = FreeFile()
            FileOpen(hnd, myFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
            FileClose(hnd)
        Catch
            Return True
        End Try
    End If
End Function

Don't know what you are using (or what this has to do with javascript), so I posted this example without testing in VB.NET. 不知道您在使用什么(或者这与javascript有什么关系),因此我发布了此示例, 但未在VB.NET中进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM