0

I have made a console application with which I need to see system information.
When I run the application, I can only see the following on the console:

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

I have written this program as console application (.net core), I don't know why I cannot see the information about my system?

My code:

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

        }
    }
}
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • and how do you run your application? seems like `p` is `null` because no `args` are passed in – Sagiv b.g Apr 25 '17 at 08:06
  • Are you starting the application with a command-line argument? Go to project properties, and under Debug tab you will see **Start Options**. Enter one of your keywords (eg `cpu`) into the _Command line arguments:_ field. – Serge Apr 25 '17 at 08:07
  • As a separate suggestion, declare and initialise `p` like this: `string p = null`. You can then remove the `else` statement. – Serge Apr 25 '17 at 08:08
  • what arguments did you pass? None? – Jodrell Apr 25 '17 at 08:28

3 Answers3

2

If you run this in debug (via visual studio) you will need to pass the args.

Go to Project-> Properties. Then click on the Debug tab, and fill in your arguments in the textbox called Command line arguments.

refference

If you run the actual compiled exe file, then simply add the desired argument.

For example:
c:\>appname.exe cpu

c:\>appname.exe win

c:\>appname.exe user

....

Community
  • 1
  • 1
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
1

The console output is the expected behaviour for your application. To get system information, you need to pass a parameter, e.g. like this sysinfo cpu

Edit: If you want to read the strings from the Console you could do it e.g. in a loop

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

On a sidenote, I'd then also add a case for exiting the loop.

skymon
  • 850
  • 1
  • 12
  • 19
0

You are reading args when the program starts. Did you forget to pass arguments to your app? Your switch condition is not matched with one of your cases, then it prints out as default case.

You can pass arguments in Visual Studio as answered before: https://stackoverflow.com/a/3697320/3678882

Community
  • 1
  • 1
Tunahan
  • 303
  • 4
  • 22