2017-03-18 269 views
2

我想使用.Net Core从文件中读取扩展属性,如Product Version,Author等。在.Net Core中获取文件扩展属性

有类,如FileVersionInfo是用来提供版本信息,Shell对象阅读更多有关文件等

现在,我不觉得这样的课了。如何使用.Net Core阅读这些信息?

+0

https://www.nuget.org/packages/System.IO.FileSystem/ – CodeCaster

+0

它包含FileInfo类,它提供了基本信息,而不是扩展属性 – Hitesh

回答

0

也许你可以通过FileInfo对象使用文件信息提供到.NET的核心..

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

迭代...

看到更多的公...

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

希望它有帮助..

+0

它提供了基本的FileInfo,不提供扩展属性 – Hitesh

2

FileVersionInfoNuGet很容易被发现,它被定位从一开始就 System.Diagnostics命名空间,所以你只需要安装的软件包:

Install-Package System.Diagnostics.FileVersionInfo 

,并使用这个类像往常一样,getting the file info一些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类型:

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 

一般规则是一类谷歌的全限定名,并添加corenuget它,所以你一定会得到所需要的文件与它的新位置。