RaspberryIO_26/Swan.Tiny/SwanRuntime.cs

202 lines
6.8 KiB
C#
Raw Normal View History

2019-12-09 17:25:54 +01:00
using Swan.Logging;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
namespace Swan {
/// <summary>
/// Provides utility methods to retrieve information about the current application.
/// </summary>
public static class SwanRuntime {
2019-12-10 20:01:19 +01:00
/*private static readonly Lazy<Assembly> EntryAssemblyLazy = new Lazy<Assembly>(Assembly.GetEntryAssembly);
2019-12-09 17:25:54 +01:00
private static readonly Lazy<String> CompanyNameLazy = new Lazy<String>(() => {
AssemblyCompanyAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
return attribute?.Company ?? String.Empty;
});
private static readonly Lazy<String> ProductNameLazy = new Lazy<String>(() => {
AssemblyProductAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
return attribute?.Product ?? String.Empty;
});
private static readonly Lazy<String> ProductTrademarkLazy = new Lazy<String>(() => {
AssemblyTrademarkAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyTrademarkAttribute)) as AssemblyTrademarkAttribute;
return attribute?.Trademark ?? String.Empty;
});
private static readonly String ApplicationMutexName = "Global\\{{" + EntryAssembly.FullName + "}}";
2019-12-10 20:01:19 +01:00
private static readonly Object SyncLock = new Object();*/
2019-12-09 17:25:54 +01:00
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static OperatingSystem? _oS;
2019-12-10 20:01:19 +01:00
#region Properties
2019-12-09 17:25:54 +01:00
/// <summary>
/// Gets the current Operating System.
/// </summary>
/// <value>
/// The os.
/// </value>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
public static OperatingSystem OS {
get {
if(_oS.HasValue == false) {
String windowsDirectory = Environment.GetEnvironmentVariable("windir");
_oS = String.IsNullOrEmpty(windowsDirectory) == false && windowsDirectory.Contains(@"\") && Directory.Exists(windowsDirectory)
? (OperatingSystem?)OperatingSystem.Windows
: (OperatingSystem?)(File.Exists(@"/proc/sys/kernel/ostype") ? OperatingSystem.Unix : OperatingSystem.Osx);
}
return _oS ?? OperatingSystem.Unknown;
}
}
2019-12-10 20:01:19 +01:00
/*/// <summary>
2019-12-09 17:25:54 +01:00
/// Checks if this application (including version number) is the only instance currently running.
/// </summary>
/// <value>
/// <c>true</c> if this instance is the only instance; otherwise, <c>false</c>.
/// </value>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Boolean IsTheOnlyInstance {
get {
lock(SyncLock) {
try {
// Try to open existing mutex.
_ = Mutex.OpenExisting(ApplicationMutexName);
} catch {
try {
// If exception occurred, there is no such mutex.
Mutex appMutex = new Mutex(true, ApplicationMutexName);
$"Application Mutex created {appMutex} named '{ApplicationMutexName}'".Debug(typeof(SwanRuntime));
// Only one instance.
return true;
} catch {
// Sometimes the user can't create the Global Mutex
}
}
// More than one instance.
return false;
}
}
}
/// <summary>
/// Gets a value indicating whether this application instance is using the MONO runtime.
/// </summary>
/// <value>
/// <c>true</c> if this instance is using MONO runtime; otherwise, <c>false</c>.
/// </value>
public static Boolean IsUsingMonoRuntime => Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Gets the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly.
/// </value>
public static Assembly EntryAssembly => EntryAssemblyLazy.Value;
/// <summary>
/// Gets the name of the entry assembly.
/// </summary>
/// <value>
/// The name of the entry assembly.
/// </value>
public static AssemblyName EntryAssemblyName => EntryAssemblyLazy.Value.GetName();
/// <summary>
/// Gets the entry assembly version.
/// </summary>
public static Version EntryAssemblyVersion => EntryAssemblyName.Version;
/// <summary>
/// Gets the full path to the folder containing the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly directory.
/// </value>
public static String EntryAssemblyDirectory {
get {
UriBuilder uri = new UriBuilder(EntryAssembly.CodeBase);
String path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
/// <summary>
/// Gets the name of the company.
/// </summary>
/// <value>
/// The name of the company.
/// </value>
public static String CompanyName => CompanyNameLazy.Value;
/// <summary>
/// Gets the name of the product.
/// </summary>
/// <value>
/// The name of the product.
/// </value>
public static String ProductName => ProductNameLazy.Value;
/// <summary>
/// Gets the trademark.
/// </summary>
/// <value>
/// The product trademark.
/// </value>
public static String ProductTrademark => ProductTrademarkLazy.Value;
/// <summary>
/// Gets a local storage path with a version.
/// </summary>
/// <value>
/// The local storage path.
/// </value>
public static String LocalStoragePath {
get {
String localAppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), EntryAssemblyName.Name);
String returnPath = Path.Combine(localAppDataPath, EntryAssemblyVersion.ToString());
if(!Directory.Exists(returnPath)) {
_ = Directory.CreateDirectory(returnPath);
}
return returnPath;
}
2019-12-10 20:01:19 +01:00
}*/
2019-12-09 17:25:54 +01:00
#endregion
2019-12-10 20:01:19 +01:00
/*#region Methods
2019-12-09 17:25:54 +01:00
/// <summary>
/// Build a full path pointing to the current user's desktop with the given filename.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>
/// The fully qualified location of path, such as "C:\MyFile.txt".
/// </returns>
/// <exception cref="ArgumentNullException">filename.</exception>
public static String GetDesktopFilePath(String filename) {
if(String.IsNullOrWhiteSpace(filename)) {
throw new ArgumentNullException(nameof(filename));
}
String pathWithFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), filename);
return Path.GetFullPath(pathWithFilename);
}
2019-12-10 20:01:19 +01:00
#endregion*/
2019-12-09 17:25:54 +01:00
}
}