如果() +开关() +图片框 [C#]

发布于 2025-01-20 01:47:18 字数 1566 浏览 5 评论 0原文

事情是这样的,我有一个 Windows 窗体,其中有一个空图片框和一些使用 wmi 获取系统信息的标签,图片框应该使用 if 自动从项目中名为 Pictures 的文件夹中获取操作系统的徽标声明或开关盒,但我一生都无法想出可行的东西。

基本思想是:

如果 OSN 包含 7,pictureBoxOS 从图片文件夹中获取名为 W7 的图像。

如果 OSN 包含 8,pictureBoxOS 从图片文件夹中获取名为 W8 的图像 依此类推

OSN 是操作系统名称

下面是 SysInfo 类中写入的一些内容

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

然后是表格中写入的内容

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

            
        }

        
    }


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

            
        }

        
    }


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2025-01-27 01:47:18

对于任何可能偶然发现这篇文章的人,我确实找到了一种使它起作用的方法:

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

        }

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文