事情是这样的。
我的 WinApp 正在运行,对吧? 假设进程“A”。它创建一个文件并保留句柄(保持文件打开以进行写入,这是必须的)。
然后它启动其他 msbuild 进程,我们称之为“B”。 此过程由 System.Diagnostic 启动.Process 类。
在某些时候,我的 WinApp (A) 需要删除之前创建的文件(记住它是由 A 本身创建的),此时我收到 IOException 并显示消息“进程无法访问文件‘X’,因为它正在被访问”。被另一个进程使用”。 事实上是这样!...如果我终止进程“B”,只有“A”才能成功删除该文件。
所以我的问题是:
1)有没有办法告诉我创建的进程不处理我打开的文件?
2)还有其他方法可以实现我的场景吗?
Here's the deal.
My WinApp is running, right? in let's say process 'A'.It creates a file and keeps the handle (keeps the file open for writing, this is a must).
Then it starts other msbuild process, let's call it 'B'. This process is started with the System.Diagnostic.Process class.
At some point, my WinApp (A) needs to delete the previously created file (remember it was created by A itself), and that's when I get an IOException with the message "The process cannot access the file 'X' because it is being used by another process". And it actually is!... If I terminate process 'B', only then 'A' can successfully delete the file.
So my questions are:
1) Is there a way I can tell the process I create not no handle the files I opened?
2) Is there another way to achieve my scenario?
发布评论
评论(4)
看起来像
System.Diagnostic.Process.Start
调用 CreateProcess,并将bInheritHandles
参数设置为true
。您可以尝试在 UseShellExecute 设置为 true ="nofollow noreferrer">ProcessStartInfo,或者直接P/Invoke到
CreateProcess
。It looks like
System.Diagnostic.Process.Start
calls CreateProcess with thebInheritHandles
argument set totrue
.You could try setting
UseShellExecute
to true in ProcessStartInfo, or directly P/Invoke toCreateProcess
.可以强制关闭文件句柄而不关闭正在使用该句柄的进程,但这可能会导致应用程序崩溃。 换句话说,做你想做的事可能会导致“B”崩溃。 也就是说,您所要求的绝对是可能的,因为应用程序 Process Explorer 可以做到这一点。 如果您在该链接中搜索留言板,您可能会发现它提供了丰富的信息,尽管即使查找哪个应用程序正在使用句柄的行为也是一种令人沮丧的练习,更不用说实际关闭句柄了。
It is possible to force a file handle closed without closing the process is using the handle, though this might cause the application to crash. In other words, doing what you wannt to do may cause 'B' to crash. That said, what you are asking for is definitely possible, since the application, Process Explorer can do this. I you search the messageboard in that link, you might find it informative, though even the act of finding which application is using a handle is an exercise in frustration, never mind actually closing the handle.
我不知道删除具有打开句柄的文件的有保证的方法,但是如果您可以等到系统重新启动才能删除文件,则可以使用与 来自 Sysinternals 的 MoveFile 实用程序。
该程序将注册表值添加到Windows在启动时检查的HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations键中,这将保证文件被删除,尽管不是立即删除。
I don't know of a guaranteed way to delete a file with open handles, but if you can wait until a system restart for the file to be deleted, you can use the same technique as the MoveFile utility from Sysinternals.
This program adds registry values to the HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations key which Windows checks on boot, and this will guarantee the file gets deleted, albeit not immediately.
当我尝试读取独占锁定的文件时,我遇到了类似的问题。
我试图使用以下方法来做到这一点:
完整的讨论可在:
文件被另一个进程以独占方式锁定 - MSDN 论坛讨论
希望这会有所帮助。
I had come across a similar problem when I was trying to read a file that was exclusively locked.
I was trying to do it using :
The complete discussion is available at :
File exclusively locked by another process - MSDN Forum disucssion
Hope this helps.