Created
October 8, 2018 07:36
-
-
Save ifukazoo/79af73714d50477c92c71a702f87cb70 to your computer and use it in GitHub Desktop.
write fileversion to csv format file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace FileInfos | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.ReadKey(); | |
GetFileInfoUnderDir(args[0]); | |
} | |
static string[] TargetInfos = { | |
"CompanyName", | |
"FileVersion", | |
"FileDescription", | |
"LegalCopyright", | |
"InternalName", | |
"Language", | |
"OriginalFilename", | |
"ProductName", | |
"ProductVersion", | |
}; | |
static void GetFileInfoUnderDir(string dirPath) | |
{ | |
var dirInfo = new DirectoryInfo(dirPath); | |
if (!dirInfo.Exists) | |
{ | |
return; | |
} | |
var fileInfos = dirInfo.EnumerateFiles(); | |
using (var writer = new StreamWriter(new FileStream(dirPath + Path.DirectorySeparatorChar + "GetInfo.csv", | |
FileMode.Create, FileAccess.Write, FileShare.None), Encoding.GetEncoding(932))) | |
{ | |
foreach (var fInfo in fileInfos) | |
{ | |
var ext = fInfo.Extension; | |
if (ext.ToLower() == ".exe" || ext.ToLower() == ".dll" || ext.ToLower() == ".sys") | |
{ | |
var version = GetFileVersions(fInfo.FullName); | |
writer.WriteLine($"{fInfo.Name},{version ?? ""}"); | |
} | |
} | |
} | |
} | |
static string GetFileVersions(string filePath) | |
{ | |
if (!new FileInfo(filePath).Exists) | |
{ | |
return null; | |
} | |
var verInfo = FileVersionInfo.GetVersionInfo(filePath); | |
var sb = new StringBuilder(); | |
foreach (var target in TargetInfos) | |
{ | |
var getProp = typeof(FileVersionInfo).GetProperty(target); | |
var prop = getProp.GetValue(verInfo); | |
sb.Append($",\"{prop}\""); | |
} | |
sb.Append($",\"{File.GetLastAccessTime(filePath)}\""); | |
return Regex.Replace(sb.ToString(), @"\A,", ""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment