Boilerplate code for Common Class to group the required functions, needed quite often.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using static System.String;
namespace Common
{
public static class CommonUtils
{
public static ICollection GetExternalHardDiskInfo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
ICollection lstExHDD = new List();
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive
{
Model = wmi_HD["Model"].ToString(),
Type = wmi_HD["InterfaceType"].ToString()
};
if (wmi_HD["SerialNumber"] != null)
hd.SerialNo = wmi_HD["SerialNumber"].ToString();
lstExHDD.Add(hd);
}
return lstExHDD;
}
public static string CalculateMD5Hash(string FilePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(FilePath))
{
var result = Convert.ToBase64String(md5.ComputeHash(stream));
return result;
}
}
}
public static DateTimeOffset GetAESTDateTimeOffset()
{
var utc = new DateTimeOffset(DateTime.UtcNow);
var aest = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
return TimeZoneInfo.ConvertTime(utc, aest);
}
public static string GetAESTDate(string format)
{
var dateTimeOffset = GetAESTDateTimeOffset();
return dateTimeOffset.LocalDateTime.ToString();
}
public static string FormatByteSize(long i)
{
// Get absolute value
long absolute_i = (i < 0 ? -i : i);
// Determine the suffix and readable value
string suffix;
double readable;
if (absolute_i >= 0x1000000000000000) // Exabyte
{
suffix = "EB";
readable = (i >> 50);
}
else if (absolute_i >= 0x4000000000000) // Petabyte
{
suffix = "PB";
readable = (i >> 40);
}
else if (absolute_i >= 0x10000000000) // Terabyte
{
suffix = "TB";
readable = (i >> 30);
}
else if (absolute_i >= 0x40000000) // Gigabyte
{
suffix = "GB";
readable = (i >> 20);
}
else if (absolute_i >= 0x100000) // Megabyte
{
suffix = "MB";
readable = (i >> 10);
}
else if (absolute_i >= 0x400) // Kilobyte
{
suffix = "KB";
readable = i;
}
else
{
return i.ToString("0 B"); // Byte
}
// Divide by 1024 to get fractional value
readable = (readable / 1024);
// Return formatted number with suffix
return readable.ToString("0.### ") + suffix;
}
public static string ConvertBytesToMegabytes(long bytes)
{
return $"{(bytes / 1024f) / 1024f}";
}
public static string GetKeyFromFile(string FilePath)
{
if (IsNullOrWhiteSpace(FilePath))
return Empty;
var result = FilePath.Substring(FilePath.IndexOf(@"\", 0) + 1, FilePath.Length - (FilePath.IndexOf(@"\", 0) + 1)).Replace("\\", "/");
return result;
}
public static bool FolderMatch(string[] SourceFolderArray, string[] TargetFolderArray)
{
bool result = !TargetFolderArray.Except(SourceFolderArray).Any();
return result;
}
public static string ConvertClassToXML(Object ClassObjectToConvert)
{
string xmlEncodedList;
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream))
{
new XmlSerializer(ClassObjectToConvert.GetType()).Serialize(writer, ClassObjectToConvert);
xmlEncodedList = Encoding.UTF8.GetString(stream.ToArray());
}
}
return xmlEncodedList;
}
public static long CalculateLocalFilesListSize(List LocalFilesList)
{
long totalFileSize = 0;
FileInfo f = null;
LocalFilesList.ForEach(x =>
{
f = new FileInfo(x);
totalFileSize += f.Length;
});
return totalFileSize;
}
public static IEnumerable GetAllFilesFromPath(string FilePath)
{
if (Directory.Exists(FilePath))
{
var query = new DirectoryInfo(FilePath).GetFiles("*.*", SearchOption.AllDirectories);
var result = query.Select(x => Path.GetFullPath(x.FullName));
return result;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using static System.String;
namespace Common
{
public static class CommonUtils
{
public static ICollection
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
ICollection
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive
{
Model = wmi_HD["Model"].ToString(),
Type = wmi_HD["InterfaceType"].ToString()
};
if (wmi_HD["SerialNumber"] != null)
hd.SerialNo = wmi_HD["SerialNumber"].ToString();
lstExHDD.Add(hd);
}
return lstExHDD;
}
public static string CalculateMD5Hash(string FilePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(FilePath))
{
var result = Convert.ToBase64String(md5.ComputeHash(stream));
return result;
}
}
}
public static DateTimeOffset GetAESTDateTimeOffset()
{
var utc = new DateTimeOffset(DateTime.UtcNow);
var aest = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
return TimeZoneInfo.ConvertTime(utc, aest);
}
public static string GetAESTDate(string format)
{
var dateTimeOffset = GetAESTDateTimeOffset();
return dateTimeOffset.LocalDateTime.ToString();
}
public static string FormatByteSize(long i)
{
// Get absolute value
long absolute_i = (i < 0 ? -i : i);
// Determine the suffix and readable value
string suffix;
double readable;
if (absolute_i >= 0x1000000000000000) // Exabyte
{
suffix = "EB";
readable = (i >> 50);
}
else if (absolute_i >= 0x4000000000000) // Petabyte
{
suffix = "PB";
readable = (i >> 40);
}
else if (absolute_i >= 0x10000000000) // Terabyte
{
suffix = "TB";
readable = (i >> 30);
}
else if (absolute_i >= 0x40000000) // Gigabyte
{
suffix = "GB";
readable = (i >> 20);
}
else if (absolute_i >= 0x100000) // Megabyte
{
suffix = "MB";
readable = (i >> 10);
}
else if (absolute_i >= 0x400) // Kilobyte
{
suffix = "KB";
readable = i;
}
else
{
return i.ToString("0 B"); // Byte
}
// Divide by 1024 to get fractional value
readable = (readable / 1024);
// Return formatted number with suffix
return readable.ToString("0.### ") + suffix;
}
public static string ConvertBytesToMegabytes(long bytes)
{
return $"{(bytes / 1024f) / 1024f}";
}
public static string GetKeyFromFile(string FilePath)
{
if (IsNullOrWhiteSpace(FilePath))
return Empty;
var result = FilePath.Substring(FilePath.IndexOf(@"\", 0) + 1, FilePath.Length - (FilePath.IndexOf(@"\", 0) + 1)).Replace("\\", "/");
return result;
}
public static bool FolderMatch(string[] SourceFolderArray, string[] TargetFolderArray)
{
bool result = !TargetFolderArray.Except(SourceFolderArray).Any();
return result;
}
public static string ConvertClassToXML(Object ClassObjectToConvert)
{
string xmlEncodedList;
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream))
{
new XmlSerializer(ClassObjectToConvert.GetType()).Serialize(writer, ClassObjectToConvert);
xmlEncodedList = Encoding.UTF8.GetString(stream.ToArray());
}
}
return xmlEncodedList;
}
public static long CalculateLocalFilesListSize(List
{
long totalFileSize = 0;
FileInfo f = null;
LocalFilesList.ForEach(x =>
{
f = new FileInfo(x);
totalFileSize += f.Length;
});
return totalFileSize;
}
public static IEnumerable
{
if (Directory.Exists(FilePath))
{
var query = new DirectoryInfo(FilePath).GetFiles("*.*", SearchOption.AllDirectories);
var result = query.Select(x => Path.GetFullPath(x.FullName));
return result;
}
return null;
}
}
}