我如何在控制台应用程序中调用方法

时间:2016-09-28 21:13:11

标签: c# .net console-application

所以我有一个方法,它的作用是它基本上得到我的电脑的处理器名称..

现在,如果我在控制台应用程序中执行此操作,我将如何做同样的事情。

这是我尝试的但它没有打印出任何东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;    
using System.Management;

namespace testereeee
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello World");
            Console.ReadKey();
        }

        private string GetProcessorName()
        {
            string ProcessorName = "";
            ManagementObjectSearcher mos
            = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");

            foreach (ManagementObject mo in mos.Get())
                ProcessorName = mo["Name"].ToString();
            Console.WriteLine(ProcessorName.ToString());
            return ProcessorName;
        }        
    }
}

1 个答案:

答案 0 :(得分:1)

它没有打印任何东西,因为它从未被调用,更新你的代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;    
using System.Management;

namespace testereeee
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello World");
            GetProcessorName()

            Console.ReadKey();
        }

        private static string GetProcessorName()
        {
            string ProcessorName = "";
            ManagementObjectSearcher mos
            = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");

            foreach (ManagementObject mo in mos.Get())
                ProcessorName = mo["Name"].ToString();
            Console.WriteLine(ProcessorName.ToString());
            return ProcessorName;
        }        
    }
}