Home > OS >  if() switch() pictureBox [C#]
if() switch() pictureBox [C#]

Time:04-13

Here's the thing, I have a Windows Form with an empty pictureBox in it and some labels that get system info using wmi, the picture box is supposed to automatically get the logo of the operating system from a folder in the project called Pictures using an if statement or a switch case but I can't for the life of me come up with something that works.

The basic idea is :

if OSN contains a 7, pictureBoxOS gets the image called W7 from the pictures folder.

if OSN contains an 8, pictureBoxOS gets the image called W8 from the pictures folder so on and so forth

OSN being the OS Name

What follows is some of what's written in the SysInfo Class

public static class SysInfo
    {
        public static String GetOSName()
        {

            ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            String OSN = String.Empty;
            foreach (ManagementObject mo in moc)
            {

                OSN = mo.Properties["Caption"].Value.ToString();
                break;
            }
            return OSN;
     }

Then comes what's written in the form

public partial class FormSystemInfo : Form
    {
        public FormSystemInfo()
        {
            InitializeComponent();
            LoadTheme();
        }


        private void FormSystemInfo_Load(object sender, EventArgs e)
        {
            labelOSName.Text=SysInfo.GetOSName();
            labelOSVersion.Text=SysInfo.GetOSVersion();
            labelBrandModel.Text=SysInfo.GetBrandModel();
            labelProcessorName.Text=SysInfo.GetProcessorName();
            labelMemory.Text=SysInfo.GetPhysicalMemory();
            labelDriveID.Text=SysInfo.GetDriveID();
            labelGPUName.Text=SysInfo.GetGPUName();
            labelSerialNumber.Text = SysInfo.GetSerialNumber();

            
        }

        
    }


CodePudding user response:

For anyone who might stumble upon this post, I did find a way to make it work:

DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
        ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        String OSN = String.Empty;

        foreach (ManagementObject mo in moc)
        {

            OSN = mo.Properties["Caption"].Value.ToString();
            break;
        }

        if (OSN.Contains("7"))
        {
            string path = dir.Parent.Parent.Parent.FullName   @"\Pictures\W7.png";
            pictureBoxOS.Image = Image.FromFile(path);
        }
        else if (OSN.Contains("8"))
        {
            string path = dir.Parent.Parent.Parent.FullName   @"\Pictures\W8.png";
            pictureBoxOS.Image = Image.FromFile(path);
        }
        else if (OSN.Contains("10"))
        {
            string path = dir.Parent.Parent.Parent.FullName   @"\Pictures\W10.png";
            pictureBoxOS.Image = Image.FromFile(path);

        }
        else if (OSN.Contains("11"))
        {
            string path = dir.Parent.Parent.Parent.FullName   @"\Pictures\W11.png";
            pictureBoxOS.Image = Image.FromFile(path);

        }
  • Related