Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining file extended properties in .Net Core

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?

like image 708
Hitesh Avatar asked Mar 18 '17 09:03

Hitesh


2 Answers

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.

like image 139
VMAtm Avatar answered Sep 22 '22 07:09

VMAtm


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.

like image 21
user7417866 Avatar answered Sep 22 '22 07:09

user7417866