Доработать код(вывести версию операционной системы) - C#
Формулировка задачи:
Помогите пожалуйста доработать код, никак не могу заставить выводить версию ос через WinApi. Пробовал и через структуру, и через класс.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- namespace winAPIS
- {
- [StructLayout(LayoutKind.Sequential)]
- public class OSVersionInfo
- {
- public int dwOSVersionInfoSize;
- public String szCSDVersion;
- }
- class Program
- {
- [DllImport("kernel32.dll")]
- static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer,
- uint uSize);
- [DllImport("Kernel32")]
- public static extern bool GetComputerName(StringBuilder buffer, ref uint size);
- [DllImport("advapi32.dll")]
- static extern bool GetUserName(StringBuilder sb, ref uint length);
- [DllImport("kernel32.dll")]
- public static extern bool GetVersionEx([In, Out] OSVersionInfo osvi);
- [DllImport("kernel32.dll")]
- extern static short QueryPerformanceCounter(ref long x);
- [DllImport("kernel32.dll")]
- extern static short QueryPerformanceFrequency(ref long x);
- [DllImport("winmm.dll")]
- protected static extern int mciSendString
- (string mciCommand,
- StringBuilder returnValue,
- int returnLength,
- IntPtr callback);
- static void Main(string[] args)
- {
- StringBuilder buffer = new StringBuilder(128);
- StringBuilder tmp = new StringBuilder(128);
- GetSystemDirectory(tmp, 128);
- Console.WriteLine(buffer + "Системный каталог: " + tmp);
- uint l = 128;
- GetComputerName(tmp, ref l);
- Console.WriteLine(buffer + "Название компьютера: " + tmp);
- GetUserName(tmp, ref l);
- Console.WriteLine(buffer + "Имя пользователя: " + tmp);
- OSVersionInfo osvi = new OSVersionInfo();
- osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi);
- GetVersionEx(osvi);
- Console.WriteLine("Версия ОС: " + OSVersionInfo);
- long ctr1 = 0, ctr2 = 0, freq = 0;
- int acc = 0, i = 0;
- if (QueryPerformanceCounter(ref ctr1) != 0) // Begin timing.
- {
- for (i = 0; i < 100; i++) acc++; // Code being timed.
- QueryPerformanceCounter(ref ctr2); // Finish timing.
- Console.WriteLine("\nНачальное значение: " + ctr1);
- Console.WriteLine("Конечное значение: " + ctr2);
- QueryPerformanceFrequency(ref freq);
- Console.WriteLine("QueryPerformanceCounter minimum resolution: 1/" + freq + " секунд.");
- Console.WriteLine("100 Increment time: " + (ctr2 - ctr1) * 1.0 / freq + " секунда.");
- }
- else
- Console.WriteLine("High-resolution counter not supported.");
- Console.WriteLine("\nСледующая часть кода демонстрирует открытие/закрытие дисковода");
- Console.WriteLine("\nХотите начать?\n1: Да\n2: Нет");
- Console.WriteLine("\nВаш выбор: ");
- if (Console.ReadKey().Key == ConsoleKey.D1)
- {
- Console.WriteLine("\nДисковод открывается");
- int result = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
- Console.WriteLine("\nХотите закрыть Дисковод?\n1: Да\n2: Нет");
- Console.WriteLine("\nВаш выбор: ");
- if (Console.ReadKey().Key == ConsoleKey.D1)
- {
- Console.WriteLine("\nДисковод закрывается");
- result = mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
- Console.WriteLine("\nНажмите ENTER для завершения");
- }
- else
- {
- Console.WriteLine("\n\n\n\nОк!");
- Console.WriteLine("\nНажмите ENTER для завершения");
- }
- Console.Read();
- }
- }
- }
- }
Решение задачи: «Доработать код(вывести версию операционной системы)»
textual
Листинг программы
- [StructLayout(LayoutKind.Sequential)]
- public class OSVersionInfo
- {
- public int dwOSVersionInfoSize;
- public uint dwMajorVersion;
- public uint dwMinorVersion;
- public uint dwBuildNumber;
- public uint dwPlatformId;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
- public string szCSDVersion;
- }
- [DllImport("kernel32.dll")]
- public static extern bool GetVersionEx([In,Out]OSVersionInfo osvi);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д