监视文件夹并查找是否在Windows应用程序中打开了文件

编辑:显然没有简单或方便的方法来了解文件是否被进程打开/保持 。 我认为这是Windows操作系统本身的一个问题或设计决定,因为即使像Process Explorer这样的程序也无法判断我的“mytext.txt”文件是在记事本中打开的,除了它的窗口之外:)进一步的研究也只显示了获取方法这个信息可靠地落入系统驱动程序。 我的问题仍然存在,如果有人能告诉我这个驱动程序解决方案的路径,也很感激。

ps:我真诚地认为这个答案应该有一个简单的解决方案。 我的意思是,感觉它是整个操作系统中缺少的function – 无法判断文件是否打开? 真? 甚至不是API方法? 这对我来说不合适。

– 原始问题 –

我一直在网上搜索,找到答案。 显然在Windows Vista之后添加了一个名为Restart Manager的function,我们可以调用该dll的方法来检查文件是否已锁定。

但是,当我在一个简单的控制台应用程序中尝试这个时(受本文的启发: https : //blogs.msdn.microsoft.com/oldnewthing/20120217-00/?p = 8283 ,这个SO答案https://stackoverflow.com它可以判断一个office文件(xls,doc等…)是否被锁定/打开但是当我在记事本中打开.txt文件时,或者当我打开一个项目时打开.sln文件时它不会被锁定/打开在Visual Studio中。

我的程序正在监视用户系统中的预设文件夹(例如,Documents文件夹),我想在该文件夹中打开任何文件时触发事件。 程序本身不会修改文件或使用该文件,因此锁定或解锁不是问题,我只是希望在受监视文件夹中的任何程序中打开文件时收到通知。

现在,我正在使用此代码来查找哪些进程正在锁定文件,但我觉得有一种更简单的方法可以知道何时打开文件而不是使用Win32 apis。

static public class FileUtil { [StructLayout(LayoutKind.Sequential)] struct RM_UNIQUE_PROCESS { public int dwProcessId; public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime; } const int RmRebootReasonNone = 0; const int CCH_RM_MAX_APP_NAME = 255; const int CCH_RM_MAX_SVC_NAME = 63; enum RM_APP_TYPE { RmUnknownApp = 0, RmMainWindow = 1, RmOtherWindow = 2, RmService = 3, RmExplorer = 4, RmConsole = 5, RmCritical = 1000 } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] struct RM_PROCESS_INFO { public RM_UNIQUE_PROCESS Process; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)] public string strAppName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)] public string strServiceShortName; public RM_APP_TYPE ApplicationType; public uint AppStatus; public uint TSSessionId; [MarshalAs(UnmanagedType.Bool)] public bool bRestartable; } [DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)] static extern int RmRegisterResources(uint pSessionHandle, UInt32 nFiles, string[] rgsFilenames, UInt32 nApplications, [In] RM_UNIQUE_PROCESS[] rgApplications, UInt32 nServices, string[] rgsServiceNames); [DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)] static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey); [DllImport("rstrtmgr.dll")] static extern int RmEndSession(uint pSessionHandle); [DllImport("rstrtmgr.dll")] static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded, ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps, ref uint lpdwRebootReasons); ///  /// Find out what process(es) have a lock on the specified file. ///  /// Path of the file. /// Processes locking the file /// See also: /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373661(v=vs.85).aspx /// http://wyupdate.googlecode.com/svn-history/r401/trunk/frmFilesInUse.cs (no copyright in code at time of viewing) /// ///  static public List WhoIsLocking(string path) { uint handle; string key = Guid.NewGuid().ToString(); List processes = new List(); int res = RmStartSession(out handle, 0, key); if (res != 0) throw new Exception("Could not begin restart session. Unable to determine file locker."); try { const int ERROR_MORE_DATA = 234; uint pnProcInfoNeeded = 0, pnProcInfo = 0, lpdwRebootReasons = RmRebootReasonNone; string[] resources = new string[] { path }; // Just checking on one resource. res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null); if (res != 0) throw new Exception("Could not register resource."); //Note: there's a race condition here -- the first call to RmGetList() returns // the total number of process. However, when we call RmGetList() again to get // the actual processes this number may have increased. res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons); if (res == ERROR_MORE_DATA) { // Create an array to store the process results RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded]; pnProcInfo = pnProcInfoNeeded; // Get the list res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons); if (res == 0) { processes = new List((int)pnProcInfo); // Enumerate all of the results and add them to the // list to be returned for (int i = 0; i < pnProcInfo; i++) { try { processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId)); } // catch the error -- in case the process is no longer running catch (ArgumentException) { } } } else throw new Exception("Could not list processes locking resource."); } else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result."); } finally { RmEndSession(handle); } return processes; } } 

任何帮助,将不胜感激。

您是否需要知道谁打开了文件,或者只是需要知道它是否被锁定。

如果您只是需要知道它是否被锁定,您可以使用FileSystemWatcher。 此组件将监视某个文件夹以进行各种更改。