18

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?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Hitesh
  • 1,067
  • 1
  • 10
  • 27

2 Answers2

15

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.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
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://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers

Hope it helps.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Abhinav
  • 1,202
  • 1
  • 8
  • 12