Nel mio scenario avevo bisogno la mia domanda per acquisire informazioni sul computer per possibili segnalazioni di bug e statistiche.
Non ho trovato le soluzioni in cui un manifest di applicazione doveva essere aggiunto soddisfacente. La maggior parte dei suggerimenti che ho trovato durante la ricerca su google ha suggerito proprio questo, sfortunatamente.
In questo caso, quando si utilizza un manifest, è necessario aggiungere manualmente ogni versione del sistema operativo affinché questa particolare versione del sistema operativo sia in grado di segnalarsi in fase di esecuzione.
In altre parole, questo diventa una condizione di competizione: un utente della mia app potrebbe utilizzare una versione della mia app che pre-data il sistema operativo in uso. Dovrei aggiornare l'app immediatamente quando una nuova versione del sistema operativo è stata lanciata da Microsoft.Dovrei anche costringere gli utenti ad aggiornare l'app nello stesso momento in cui hanno aggiornato il sistema operativo.
In altre parole, non molto fattibile.
Dopo aver sfogliato le opzioni ho trovato alcuni riferimenti (sorprendentemente pochi rispetto al manifest dell'app) che suggerivano invece l'uso delle ricerche di registro.
mio (abbattuto) ComputerInfo
classe con solo WinMajorVersion
, WinMinorVersion
e IsServer
proprietà si presenta così:
using Microsoft.Win32;
namespace Inspection
{
/// <summary>
/// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
/// </summary>
public static class ComputerInfo
{
/// <summary>
/// Returns the Windows major version number for this computer.
/// </summary>
public static uint WinMajorVersion
{
get
{
dynamic major;
// The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
{
return (uint) major;
}
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint majorAsUInt;
return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
}
}
/// <summary>
/// Returns the Windows minor version number for this computer.
/// </summary>
public static uint WinMinorVersion
{
get
{
dynamic minor;
// The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
out minor))
{
return (uint) minor;
}
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint minorAsUInt;
return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
}
}
/// <summary>
/// Returns whether or not the current computer is a server or not.
/// </summary>
public static uint IsServer
{
get
{
dynamic installationType;
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
out installationType))
{
return (uint) (installationType.Equals("Client") ? 0 : 1);
}
return 0;
}
}
private static bool TryGetRegistryKey(string path, string key, out dynamic value)
{
value = null;
try
{
using(var rk = Registry.LocalMachine.OpenSubKey(path))
{
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
}
catch
{
return false;
}
}
}
}
Questo potrebbe essere rilevante: dalla pagina che hai collegato a "Applicazioni non manifestate per Windows 8.1 o Windows 10 restituirà il valore della versione del sistema operativo Windows 8 (6.2)." – stuartd
Suppongo che tu abbia ragione, l'ho persa –
La mia applicazione è console, quindi ho solo questo file App.config (nessun gruppo) –