栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#中找出修改过的用户名文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#中找出修改过的用户名文件

我不记得我在哪里找到了这段代码,但是它是使用pInvoke的替代方法,我认为这对于执行此任务有些过分了。使用

FileSystemWatcher
监视文件夹,当事件触发时,您可以使用以下代码确定哪个用户更改了文件:

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参考

这是有关如何调用该方法的一些示例:

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);

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

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

注意:此解决方案已在Windows 7和Windows
10上进行了测试。除非使用Shell32获取文件扩展属性时按照Exception在
STA上运行,否则该解决方案将无法工作,并且您将看到以下错误:

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400780.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号