如何强制删除被锁文件?急!!

cisso 2009-11-11 05:38:13
请教高手如何像Unlocker一样强制删除被锁定文件啊。
我用了DuplicateHandle,然后CloseHandle,可是文件还是被锁定。。。
...全文
443 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvchyg 2011-11-24
  • 打赏
  • 举报
回复
把Execle所有进程关闭,就可以删除了。
tiancaikk 2009-11-26
  • 打赏
  • 举报
回复
把unlocker打进包里,直接调用就可以~
wuyq11 2009-11-13
  • 打赏
  • 举报
回复
qldsrx 2009-11-13
  • 打赏
  • 举报
回复
你应该找下原因,什么地方把文件锁定了,贴代码分析才是最重要的。
cisso 2009-11-13
  • 打赏
  • 举报
回复
回findcaiyzh,谢谢。
我现在的问题是我自己的进程把自己的文件锁定了,还是时不时出现一次,所以想做一个和unlocker一样的,进程稳不稳定无所谓,如果还要重启电脑的话我干脆重启进程来得简单啊。
cisso 2009-11-13
  • 打赏
  • 举报
回复
代码很复杂,我也就不贴了。。
tushadongjing 2009-11-11
  • 打赏
  • 举报
回复
有点难,看看了
宝_爸 2009-11-11
  • 打赏
  • 举报
回复
你看这种方法行不行:

In your case, you need check all process handler list to find which process locks this file and then close the handler via DLL injection. But it may cause other process unstable. So using Windows API MoveFileEx is an acceptable solution. This API can accept a parameter “MOVEFILE_DELAY_UNTIL_REBOOT ” to remove the file only after reboot. Behind this API, it just adds a value to record the pending deleted files under the registry entry “[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations]”. Windows will detect this registry entry after reboot and delete all recorded files.

P/Invoke allows us to use managed code to call an unmanaged function implemented in an unmanaged dll. For more information about P/Invoke, please see " Platform Invoke Tutorial ". The example code below can show you how to call MoveFileEx via C#:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;



namespace ConsoleApplication1

{

class Program

{

[Flags ]

enum MoveFileFlags

{

MOVEFILE_REPLACE_EXISTING = 0x00000001,

MOVEFILE_COPY_ALLOWED = 0x00000002,

MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,

MOVEFILE_WRITE_THROUGH = 0x00000008,

MOVEFILE_CREATE_HARDLINK = 0x00000010,

MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020

}

[DllImport ("kernel32.dll" , SetLastError = true , CharSet = CharSet .Unicode)]

static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,

MoveFileFlags dwFlags);

static void Main(string [] args)

{

string fileName = @"e:\1.wav" ;

MoveFileEx(fileName, null , MoveFileFlags .MOVEFILE_DELAY_UNTIL_REBOOT);

}

}

}



111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧