使用VB.net删除另一个进程正在使用的文件? [英] Delete a file that is in use by another process with VB.net?

查看:273
本文介绍了使用VB.net删除另一个进程正在使用的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我写了一些代码来删除我电脑上某些文件夹中的文件列表。



我遇到一个小问题,其中一个文件被另一个进程打开而且无法删除。



有谁知道任何vb代码这有助于解决我遇到的这个问题吗?



这是我的代码如下:



Hi,

I have a wrote some code to delete a list of files from certain folders on my pc.

I have encountered a small problem where a file is open by another process and it can not be deleted.

Does anyone know of any vb code that would help sort this problem I am having?

Here is my code below:

Try
              Dim F2 As Short = FreeFile()
              For Each foundFile2 As String In My.Computer.FileSystem.GetFiles( _
                  "myfilepath", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                  FileOpen(F2, foundFile2, OpenMode.Binary, OpenAccess.Read, OpenShare.LockReadWrite)
                  FileClose(F2)
                  My.Computer.FileSystem.DeleteFile(foundFile2)
              Next
          Catch ex As Exception
              EnvCode = 101
              WriteLine(ex.Message)
          End Try





谢谢。



thanks.

推荐答案

访问此处...



http://www.vbforums.com/showthread.php?658181-RESOLVED-Process-cannot-acce ss-a-file-because-it-it-used-by-another-process [ ^ ]
visit here...

http://www.vbforums.com/showthread.php?658181-RESOLVED-Process-cannot-access-a-file-because-it-is-being-used-by-another-process[^]


嗯,这并不容易。

对于一个可删除的文件,它必须没有打开的句柄。

你可以终止或终止这些句柄的进程,或者关闭句柄本身。小心,其他过程不会意识到这一点。您可能会崩溃重要的应用程序 - 但最好还是杀死它。仍然,这是你的关注:)



因此,假设你可以确定哪个文件被锁定,你在这里有一个完整的描述,如何收集所有句柄,如何过滤文件句柄并找到您要查找的文件。当你有把柄时,你可以关闭它。

请参阅: http:// forum .sysinternals.com / howto-enumerate-handles_topic18892.html [ ^ ]



这不是VB.NET。你可能需要大量的P / Invoke,所以这可能会有所帮助: NtQuerySystemInformation API工作在32位而不是64位? [ ^ ]
Well, that's not easy.
For a file to be deletable, there has to be no open handles to it.
You can either terminate or kill the processes helding those handles, or close the handle itself. Be careful, the other process won't be aware of this. You might crash important applications - but could be better than killing it anyway. Still, that's your concern :)

So, assuming that you can determine which file is locked, you have a complete description here, how to gather all handles, how to filter the file handles and to find the file you are looking for. When you have the handle, you can close it.
See: http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html[^]

It is not VB.NET. You will probably need a lot of P/Invoke, so this might be helpful: NtQuerySystemInformation API Works on 32bit but not on 64bits?[^]


1)您应该尝试找到该文件的默认程序(可执行文件):



Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot

Dim secondKey As String

Dim rtn As String =""



如果不是ext.StartsWith("。")那么

ext ="。" &安培;分钟

结束如果



key = key.OpenSubKey(分机)



如果密钥IsNot Nothing那么

secondKey = key.GetValue("")。ToString()



如果secondKey<> ; ""然后

key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(secondKey&" ShellOpenCommand")

rtn = key.GetValue("")。ToString ()

rtn = System.Text.RegularExpressions.Regex.Match(rtn,"""(。*?)""")。Groups(0).Value

结束如果

结束如果



返回rtn

结束功能


2)终止文件的默认过程

3)继续......
1) You should Try to find the default program(executable) of the file:

Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
Dim secondKey As String
Dim rtn As String = ""

If Not ext.StartsWith(".") Then
ext = "." & ext
End If

key = key.OpenSubKey(ext)

If key IsNot Nothing Then
secondKey = key.GetValue("").ToString()

If secondKey <> "" Then
key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(secondKey & "ShellOpenCommand")
rtn = key.GetValue("").ToString()
rtn = System.Text.RegularExpressions.Regex.Match(rtn, """(.*?)""").Groups(0).Value
End If
End If

Return rtn
End Function

2) Kill the default process of the file
3) Continue...


这篇关于使用VB.net删除另一个进程正在使用的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆