在.Net Core中获取文件扩展属性 [英] Obtaining file extended properties in .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
可以在 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.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中获取文件扩展属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!