Как запустить код? - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день. Господа, прошу помощи. Знаний не достаточно, приходится действовать, что называется "вплавь" Прошу помочь с кодом. Как его запустить? Суть в следующем: Берутся данные из реестра о подключенных мониторах с серийными номерами. Данная информация записывается в файл на диске D:.. Запустить никак не могу..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.IO;
 
namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
         
        }
        private void MonitorInfo()
        {
            Microsoft.Win32.RegistryKey Display = Microsoft.Win32.Registry.LocalMachine;
            Boolean bFailed = false;
            try
            {
                Display = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\DISPLAY");
            }
            catch
            {
                bFailed = true;
            }
                        if (!bFailed & (Display != null))
            {
                foreach (string sMonitorID in Display.GetSubKeyNames())
                {
                    RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);
                    if (MonitorID != null)
                    {
                        foreach (string sPNPID in MonitorID.GetSubKeyNames())
                        {
                            RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
                            if (PnPID != null)
                            {
                                string[] sSubkeys = PnPID.GetSubKeyNames();
                                // if (sSubkeys.Contains("Control"))
                                {
                                    if (sSubkeys.Contains("Device Parameters"))
                                    {
                                        RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
                                        string sSerial = "";
                                        string sModel = "";
                                        string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
                                        string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });
                                        byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
                                        if (bObj != null)
                                        {
                                            string[] sDescriptor = new string[4];
                                            Encoding ANSI = Encoding.GetEncoding(1252);
                                            sDescriptor[0] = ANSI.GetString(bObj, 0x36, 18);
                                            sDescriptor[1] = ANSI.GetString(bObj, 0x48, 18);
                                            sDescriptor[2] = ANSI.GetString(bObj, 0x5A, 18);
                                            sDescriptor[3] = ANSI.GetString(bObj, 0x6C, 18);
                                            //Search the Keys
                                            foreach (string sDesc in sDescriptor)
                                            {
                                                if (sDesc.Contains(sSerFind))
                                                {
                                                    sSerial = sDesc.Substring(4).Replace("\0", "").Trim(); //4
                                                }
                                                if (sDesc.Contains(sModFind))
                                                {
                                                    sModel = sDesc.Substring(4).Replace("\0", "").Trim(); //4
                                                }
                                            }
                                        }
                                        if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
                                        {
                                            string fileName = "D:/monitors.txt";
                                            FileStream aFile = new FileStream(fileName, FileMode.OpenOrCreate);
                                            StreamWriter sw = new StreamWriter(aFile);
                                            aFile.Seek(0, SeekOrigin.End);
                                            sw.WriteLine("Модель: " + sModel);
                                            sw.WriteLine("Серийный номер: " + sSerial);
                                            sw.Close();
                                            //MessageBox.Show("Модель: " + sModel + Environment.NewLine + "Серийный номер: " + sSerial);
                                          
                                        }
                                    }
                                }
                            }
                        }
                    }
 
                }
            }
        }
    } 
    }
Так же в коде присутствует: if (sSubkeys.Contains("Control"))
   foreach (string sPNPID in MonitorID.GetSubKeyNames())
                        {
                            RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
                            if (PnPID != null)
                            {
                                string[] sSubkeys = PnPID.GetSubKeyNames();
                                // if (sSubkeys.Contains("Control"))
                                {
                                    if (sSubkeys.Contains("Device Parameters"))
Когда он закомментирован, Все работает... Когда не закомментирован - результат не выходит почему то... Данный код нужно запускать из под консоли... Спасибо заранее

Решение задачи: «Как запустить код?»

textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
 
namespace ConsoleApp8
{
 
    class Program
    {
        static void Main(string[] args)
        {
            MonitorInfo();
        }
        private static void MonitorInfo()
        {
            Microsoft.Win32.RegistryKey Display = Microsoft.Win32.Registry.LocalMachine;
            Boolean bFailed = false;
            try
            {
                Display = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\DISPLAY");
            }
            catch
            {
                bFailed = true;
            }
 
            var result = new Dictionary<string, string>();
 
            if (!bFailed & (Display != null))
            {
                foreach (string sMonitorID in Display.GetSubKeyNames())
                {
                    RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);
                    if (MonitorID != null)
                    {
                        foreach (string sPNPID in MonitorID.GetSubKeyNames())
                        {
                            RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
                            if (PnPID != null)
                            {
                                string[] sSubkeys = PnPID.GetSubKeyNames();
                                
                                if (sSubkeys.Contains("Device Parameters"))
                                {
                                    RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
                                    string sSerial = "";
                                    string sModel = "";
                                    string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
                                    string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });
                                    byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
                                    if (bObj != null)
                                    {
                                        string[] sDescriptor = new string[4];
                                        Encoding ANSI = Encoding.GetEncoding(1252);
                                        sDescriptor[0] = ANSI.GetString(bObj, 0x36, 18);
                                        sDescriptor[1] = ANSI.GetString(bObj, 0x48, 18);
                                        sDescriptor[2] = ANSI.GetString(bObj, 0x5A, 18);
                                        sDescriptor[3] = ANSI.GetString(bObj, 0x6C, 18);
                                        //Search the Keys
                                        foreach (string sDesc in sDescriptor)
                                        {
                                            if (sDesc.Contains(sSerFind))
                                            {
                                                sSerial = sDesc.Substring(4).Replace("\0", "").Trim(); //4
                                            }
                                            if (sDesc.Contains(sModFind))
                                            {
                                                sModel = sDesc.Substring(4).Replace("\0", "").Trim(); //4
                                            }
                                        }
                                    }
                                    if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
                                    {
                                        if (!result.ContainsKey(sSerial))
                                        {
                                            result.Add(sSerial, sModel);
                                        }
                                    }
                                }
                                
                            }
                        }
                    }
 
                }
            }
 
            if (result.Any())
            {
                string fileName = "D:/monitors.txt";
                FileStream aFile = new FileStream(fileName, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(aFile);
                aFile.Seek(0, SeekOrigin.End);
 
                foreach (var keyValue in result)    
                {
                    sw.WriteLine("Модель: " + keyValue.Value);
                    sw.WriteLine("Серийный номер: " + keyValue.Key);
                }
               
                sw.Close();
            }
        }
    }
}

Оцени полезность:

14   голосов , оценка 4.429 из 5
Похожие ответы