2017-03-18 268 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它,所以你一定會得到所需要的文件與它的新位置。