简体   繁体   English

if() + switch() + 图片框 [C#]

[英]if() + switch() + pictureBox [C#]

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.事情是这样的,我有一个 Windows 表单,里面有一个空的图片框和一些使用 wmi 获取系统信息的标签,图片框应该使用 if 从项目中名为 Pictures 的文件夹自动获取操作系统的徽标声明或开关盒,但我无法为我的生活想出一些有用的东西。

The basic idea is:基本思想是:

if OSN contains a 7, pictureBoxOS gets the image called W7 from the pictures folder.如果 OSN 包含 7,则 pictureBoxOS 从图片文件夹中获取名为 W7 的图像。

if OSN contains an 8, pictureBoxOS gets the image called W8 from the pictures folder so on and so forth如果 OSN 包含 8,则 pictureBoxOS 从图片文件夹中获取名为 W8 的图像,依此类推

OSN being the OS Name OSN 是操作系统名称

What follows is some of what's written in the SysInfo Class以下是 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();

            
        }

        
    }


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);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM