Obtaining file extended properties in .Net Core

最后都变了- 提交于 2021-01-02 04:56:06

问题


I want to read extended properties like Product Version, Author, etc. from a file using .Net Core.

There were classes like FileVersionInfo that used to provide version information, Shell object to read more about file, etc.

Now, I don't find such classes any more. How do I read such info using .Net Core?


回答1:


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

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

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

after that usage is similar:

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

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

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.




回答2:


probably you can use File Info Provider into .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

Iterate through fileInfo object.

See this for more information:

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

Hope it helps.



来源:https://stackoverflow.com/questions/42872823/obtaining-file-extended-properties-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!