在 .Net Core 中获取文件扩展属性 [英] Obtaining file extended properties in .Net Core
问题描述
我想使用 .Net Core
从文件中读取扩展属性,例如 Product Version
、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
并像往常一样使用这个类,getting来自某些的文件信息IFileProvider
,例如PhysicalFileProvider
:
and use this class as usual, getting the file info from some IFileProvider
, for example, 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
对于 作者
信息,您应该使用 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 中获取文件扩展属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!