static public supportedOSs ManifestSupportedOSs( ) { supportedOSs OSs = supportedOSs.None; Assembly asm = Assembly.ReflectionOnlyLoad(Assembly.GetCallingAssembly( ).FullName); string containingFileName = asm.GetName( ).Name + ".app.manifest"; XmlDocument doc = new XmlDocument( ); using (Stream stream = asm.GetManifestResourceStream(containingFileName)) { doc.Load(stream); XmlNodeList elem = doc.GetElementsByTagName("supportedOS"); foreach (XmlLinkedNode node in elem) { foreach (XmlNode attr in node.Attributes) { string value = attr.Value.Replace(" ", "").Replace("-", ""); if (value.Contains("e2011457-1546-43c5-a5fe-008deee3d3f0")) { OSs |= supportedOSs.Vista; } else if (value.Contains("35138b9a-5d96-4fbd-8e2d-a2440225f93a")) { OSs |= supportedOSs.Windows7; } else if (value.Contains("4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38")) { OSs |= supportedOSs.Windows8; } else if (value.Contains("1f676c76-80e1-4239-95bb-83d0f6d0da78")) { OSs |= supportedOSs.Windows81; } else if (value.Contains("8e0f7a12bfb34fe8b9a548fd50a15a9a")) { OSs |= supportedOSs.Windows10; } } } } return(OSs); }
private void Init( ) { if (!initiated) { initiated = true; uint mode; handle = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleMode(handle, out mode); mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(handle, mode); var osver = System.Environment.OSVersion; osversionmajor = osver.Version.Major; osversionminor = osver.Version.Minor; osversionbuild = osver.Version.Build; supportedOSs OSs = ManifestSupportedOSs( ); if (!OSs.HasFlag(supportedOSs.Windows10)) { Console.WriteLine("<supportedOS Id=\"{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}\" /> not found in manifest"); Console.WriteLine("OSVersion can not be checked then the virtual terminal will be disabled"); } else { if (osversionmajor >= 10 && osversionminor > 0) { CanEnableVirtualTerminal = true; return; } else if (osversionmajor >= 10 && osversionbuild >= 10586) { CanEnableVirtualTerminal = true; return; } else { Console.WriteLine("Virtual terminal escape sequences are only available in Windows 10 Build 10586 or newer "); Console.WriteLine("Virtual terminal will be disabled"); } } CanEnableVirtualTerminal = false; } }