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

Hit*_*esh 15 c# file .net-core asp.net-core

我想读扩展属性一样Product Version,Author使用一个文件,等等.Net Core.

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

现在,我再也找不到这样的课了.我如何使用这些信息阅读.Net Core

VMA*_*Atm 11

FileVersionInfo可以轻松地在NuGet上找到,它System.Diagnostics从一开始就位于 名称空间中,因此您只需要安装软件包即可:

Install-Package System.Diagnostics.FileVersionInfo
Run Code Online (Sandbox Code Playgroud)

并像往常一样使用此类,从some中获取文件信息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
Run Code Online (Sandbox Code Playgroud)

有关Author信息,您应该使用FileSecurity位于System.Security.AccessControl名称空间中的class,其类型为System.Security.Principal.NTAccount

Install-Package System.Security.AccessControl
Install-Package System.Security.Principal
Run Code Online (Sandbox Code Playgroud)

之后的用法类似:

using System.Security.AccessControl;
using System.Security.Principal;

var fileSecurity = new FileSecurity(fileInfo.PhysicalPath, AccessControlSections.All);
// fileSecurity.GetOwner(typeof(NTAccount)) is available here
Run Code Online (Sandbox Code Playgroud)

现在的一般规则是,用Google搜索某个类的全限定名称并添加core或添加nuget到该类,因此,您肯定会在其新位置获得需要的文件。


小智 2

也许你可以在.net core中使用文件信息提供程序..

IFileProvider provider = new PhysicalFileProvider(applicationRoot);
IDirectoryContents contents = provider.GetDirectoryContents(""); // the applicationRoot contents
IFileInfo fileInfo = provider.GetFileInfo("wwwroot/js/site.js"); // a file under applicationRoot
Run Code Online (Sandbox Code Playgroud)

迭代fileInfo对象。

请参阅此了解更多信息:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers

希望能帮助到你。

  • 它提供基本的 FileInfo,不提供扩展属性 (2认同)