在.Net Core中获取文件扩展属性

编程入门 行业动态 更新时间:2024-10-21 13:37:05
本文介绍了在.Net Core中获取文件扩展属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用 .Net Core 从文件中读取扩展属性,例如产品版本, Author 等.

I want to read extended properties like Product Version, Author, etc. from a file using .Net Core.

诸如 FileVersionInfo 之类的类用于提供版本信息,Shell对象用于读取有关文件的更多信息,等等.

There were classes like FileVersionInfo that used to provide version information, Shell object to read more about file, etc.

现在,我再也找不到此类了.如何使用 .Net Core 读取此类信息?

Now, I don't find such classes any more. How do I read such info using .Net Core?

推荐答案

FileVersionInfo 可以在 NuGet ,它位于 System.Diagnostics 命名空间从一开始,所以您只需要安装该软件包:

FileVersionInfo can be easily found on NuGet, it's been located in System.Diagnostics namespace from the beginning, so you need just to install the package:

Install-Package System.Diagnostics.FileVersionInfo

并照常使用此类,获取一些 IFileProvider ,例如, PhysicalFileProvider :

using System.Diagnostics; var provider = new PhysicalFileProvider(applicationRoot); // the applicationRoot contents var contents = provider.GetDirectoryContents(""); // a file under applicationRoot var fileInfo = provider.GetFileInfo("wwwroot/js/site.js"); // version information var myFileVersionInfo = FileVersionInfo.GetVersionInfo(fileInfo.PhysicalPath); // myFileVersionInfo.ProductVersion is available here

对于 Author 信息,您应该使用 FileSecurity 类,该类位于 System.Security.AccessControl 命名空间,类型为 System.Security.Principal.NTAccount :

For Author information you should use FileSecurity class, which is located in System.Security.AccessControl namespace, with type System.Security.Principal.NTAccount:

Install-Package System.Security.AccessControl Install-Package System.Security.Principal

之后的用法类似:

using System.Security.AccessControl; using System.Security.Principal; var fileSecurity = new FileSecurity(fileInfo.PhysicalPath, AccessControlSections.All); // fileSecurity.GetOwner(typeof(NTAccount)) is available here

现在的一般规则是,用谷歌搜索一个类的全限定名,并在其中添加 core 或 nuget ,因此,您肯定会获得所需的新文件位置.

General rule right now is to google the full qualified name for a class and add core or nuget to it, so you'll definitely get needed file with it's new location.

更多推荐

在.Net Core中获取文件扩展属性

本文发布于:2023-11-14 09:28:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   文件   Net   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!