在 C# 中找出用户名(谁)修改的文件 [英] Find out username(who) modified file in C#

查看:17
本文介绍了在 C# 中找出用户名(谁)修改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FileSystemWatcher 来监视文件夹.但是当目录中发生某些事件时,我不知道如何搜索对该文件产生影响的人.我尝试使用 EventLog.它无法工作.还有另一种方法吗?

I am using a FileSystemWatcher to monitor a folder. But when there is some event happening in the directory, I don't know how to search who made a impact on that file. I tried to use EventLog. It just couldn't work. Is there another way to do it?

推荐答案

我不记得我在哪里找到这段代码,但它是使用 pInvoke 的替代方案,我认为这对于这项任务来说有点矫枉过正.使用 FileSystemWatcher 监视文件夹,当事件触发时,您可以使用以下代码确定哪个用户更改了文件:

I cant remember where I found this code but its an alternative to using pInvoke which I think is a bit overkill for this task. Use the FileSystemWatcher to watch the folder and when an event fires you can work out which user made the file change using this code:

private string GetSpecificFileProperties(string file, params int[] indexes)
{
    string fileName = Path.GetFileName(file);
    string folderName = Path.GetDirectoryName(file);
    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder objFolder;
    objFolder = shell.NameSpace(folderName);
    StringBuilder sb = new StringBuilder();

    foreach (Shell32.FolderItem2 item in objFolder.Items())
    {
        if (fileName == item.Name)
        {
            for (int i = 0; i < indexes.Length; i++)
            {
                sb.Append(objFolder.GetDetailsOf(item, indexes[i]) + ",");
            }

            break;
        }
    }

    string result = sb.ToString().Trim();
    //Protection for no results causing an exception on the `SubString` method
    if (result.Length == 0)
    {
        return string.Empty;
    }
    return result.Substring(0, result.Length - 1);
}

Shell32 是对 DLL 的引用:Microsoft Shell Controls And Automation - 它是一个 COM 引用

Shell32 is a reference to the DLL: Microsoft Shell Controls And Automation - its a COM reference

以下是您如何调用该方法的一些示例:

Here is some example's of how you call the method:

string Type = GetSpecificFileProperties(filePath, 2);
string ObjectKind = GetSpecificFileProperties(filePath, 11);
DateTime CreatedDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 4));
DateTime LastModifiedDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 3));
DateTime LastAccessDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 5));
string LastUser = GetSpecificFileProperties(filePath, 10);
string ComputerName = GetSpecificFileProperties(filePath, 53);
string FileSize = GetSpecificFileProperties(filePath, 1);

或者将多个逗号分隔的属性放在一起:

Or get multiple comma separated properties together:

string SizeTypeAndLastModDate = GetSpecificFileProperties(filePath, new int[] {1, 2, 3});

<小时>

注意:此解决方案已在 Windows 7 和 Windows 10 上进行了测试.除非按照 异常在 STA 中运行,否则它不会工作Shell32获取文件扩展属性,会看到如下错误:

无法将类型为Shell32.ShellClass"的 COM 对象转换为接口类型Shell32.IShellDispatch6"

Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'

这篇关于在 C# 中找出用户名(谁)修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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