如何通过控制台应用程序(.NET CORE)C#查看输出数据

时间:2017-04-25 08:01:41

标签: c# .net visual-studio console-application

我制作了一个控制台应用程序,我需要查看系统信息 当我运行应用程序时,我只能在控制台上看到以下内容:

    Usage: sysinfo <cpu|win|net|host|user>
    Press any key to continue . . .

我已将此程序编写为控制台应用程序(.net核心),我不知道为什么我看不到有关我系统的信息?

我的代码:

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

namespace SystemInfo
{
    class Program
    {
        class SysInfo
        {
            public string win, net, cpu;
            public string hostname, username;

            public SysInfo()
            {
                net = Environment.Version.ToString();
                win = Environment.OSVersion.ToString();
                cpu = Environment.ProcessorCount.ToString();
                hostname = Environment.MachineName.ToString();
                username = Environment.UserName.ToString();
            }
        }
        static void Main(string[] args)
        {
            string p;

            SysInfo info = new SysInfo();

            if (args.Length > 0) p = args[0];
            else p = "null";

            switch (p)
            {
                case "cpu":
                    Console.WriteLine("CPU count: {0}", info.cpu);
                    break;
                case "win":
                    Console.WriteLine("Windows Version: {0}", info.win);
                    break;
                case "net":
                    Console.WriteLine(".NET Version: {0}", info.net);
                    break;
                case "host":
                    Console.WriteLine("Hostname: {0}", info.hostname);
                    break;
                case "user":
                    Console.WriteLine("Username: {0}", info.username);
                    break;
                default:
                    Console.WriteLine("Usage: sysinfo <cpu|win|net|host|user>");
                    break;
            }

        }
    }
}

3 个答案:

答案 0 :(得分:2)

如果你在调试中运行它(通过visual studio),你需要传递args

  

转到项目 - &gt;属性。然后单击Debug选项卡,   并在名为命令行的文本框中填写您的参数   参数。

refference

如果运行实际编译的exe文件,则只需添加所需的参数即可。

例如:
c:\>appname.exe cpu

c:\>appname.exe win

c:\>appname.exe user

...

答案 1 :(得分:1)

控制台输出是应用程序的预期行为。要获取系统信息,您需要传递一个参数,例如像这样sysinfo cpu

修改:如果您想从控制台读取字符串,可以执行以下操作:在一个循环中

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

namespace SystemInfo
{
    class Program
    {
        class SysInfo
        {
            public string win, net, cpu;
            public string hostname, username;

            public SysInfo()
            {
                net = Environment.Version.ToString();
                win = Environment.OSVersion.ToString();
                cpu = Environment.ProcessorCount.ToString();
                hostname = Environment.MachineName.ToString();
                username = Environment.UserName.ToString();
            }
        }
        static void Main()
        {
            string p;

            SysInfo info = new SysInfo();


            while (true)
            {
             p = Console.ReadLine();

             switch (p)
             {
                 case "cpu":
                     Console.WriteLine("CPU count: {0}", info.cpu);
                     break;
                 case "win":
                     Console.WriteLine("Windows Version: {0}", info.win);
                     break;
                 case "net":
                     Console.WriteLine(".NET Version: {0}", info.net);
                     break;
                 case "host":
                     Console.WriteLine("Hostname: {0}", info.hostname);
                     break;
                 case "user":
                     Console.WriteLine("Username: {0}", info.username);
                     break;
                 default:
                     Console.WriteLine("Usage: sysinfo <cpu|win|net|host|user>");
                     break;
             }
            }
        }
    }
}

在旁注中,我还会添加退出循环的案例。

答案 2 :(得分:0)

程序启动时,您正在读取args。您是否忘记将参数传递给您的应用?您的开关条件与您的某个案例不匹配,然后将其打印为默认情况。

您可以在Visual Studio中传递参数,如前所述:https://stackoverflow.com/a/3697320/3678882