如何了解哪个进程删除了硬盘驱动器上的文件

How is it possible to understand which process deletes a file on the hard drive

我有以下问题。我开发了一个将设置保存在首选项文件中的应用程序。在某个时间点,其中一个文件正在被删除。无法从我的应用程序中删除此文件。

如何了解哪个进程删除了 Windows 下硬盘上的文件?

编辑: 该问题很少出现。我正在寻找一个可以 运行 作为服务或其他东西的程序,这样我就可以为应用程序打补丁,如果有人删除文件并写入它有哪个进程,我可以在 运行 时间内监视它完成。

如果您可以使用 C# 解决方案,则可以使用 Microsoft.Diagnostics.Tracing.TraceEvent nuget packagage. It's a wrapper over ETW (Event Tracing for Windows) 事件。

Windows 内核会跟踪所有内容,您可以实时获取这些跟踪信息。但有时很难将它们关联起来。

在你的例子中,你正在处理文件删除事件,但不幸的是,这些事件没有附加到它的进程信息,所以我使用了另一个事件。这是一些示例代码:

using System;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Session;

namespace TraceDeletes
{
    class Program
    {
        static void Main(string[] args)
        {
            if (TraceEventSession.IsElevated() != true)
            {
                Console.WriteLine("To turn on ETW events you need to be Administrator, please run from an Admin process.");
                return;
            }

            // we're watching that particular file
            string filePath = @"C:\temp\New Text Document.txt";
            ulong fileKey = 0;
            string processName = null;
            using (var session = new TraceEventSession("whatever"))
            {
                // handle console CTRL+C gracefully
                Console.CancelKeyPress += (sender, e) => session.Stop();

                // we filter on events we need
                session.EnableKernelProvider(
                    KernelTraceEventParser.Keywords.DiskFileIO |
                    KernelTraceEventParser.Keywords.FileIOInit);

                // this event has no process information
                session.Source.Kernel.FileIOFileDelete += data =>
                {
                    if (data.FileKey == fileKey)
                    {
                        Console.WriteLine(data.FileName + " was deleted by " + processName);
                        fileKey = 0;
                        processName = null;
                    }
                };

                // this event has process information (id, name)
                // it happens before delete, of course
                // we remember the FileKey
                session.Source.Kernel.FileIOQueryInfo += data =>
                {
                    if (string.Compare(data.FileName, filePath, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        fileKey = data.FileKey;
                        processName = data.ProcessName;
                    }
                };

                // runs forever, press CTRL+C to stop
                session.Source.Process();
            }
        }
    }
}

如果您创建 "C:\temp\New Text Document.txt" 文件并使用 Windows 资源管理器将其删除,您应该会看到:

C:\temp\New Text Document.txt was deleted by explorer

注意:ETW 当然可以使用其他语言,但使用此 .NET 库要容易得多。

Microsoft 的 Sysinternals 应该能够帮助您。

https://docs.microsoft.com/en-us/sysinternals/downloads/

在文件和磁盘实用程序下查看。有一些实用程序可以向您显示哪个进程 accesses/modifies 给定文件。

可能正在使用 Process Monitor,参数为 «operation: SetDispositionInformationFile, 结果: 成功,详情:"Delete:True"» 在您的道路上。

关于此的更多详细信息:here and here

您可以开发服务并使用 FileSystemWatcher 并监视已删除事件。 FileSystemWatcher.Deleted Event