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

        }
    }
}
4
  • and how do you run your application? seems like p is null because no args are passed in Commented Apr 25, 2017 at 8: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. Commented Apr 25, 2017 at 8:07
  • As a separate suggestion, declare and initialise p like this: string p = null. You can then remove the else statement. Commented Apr 25, 2017 at 8:08
  • what arguments did you pass? None? Commented Apr 25, 2017 at 8:28

3 Answers 3

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

....

Sign up to request clarification or add additional context in comments.

5 Comments

I mean, I want to choose in the console between cpu, win, net, host and user. When I write cpu to see information to cpu but I not want to see press any key to countinue ... I want to refresh the message and want to write another options ?
Console.WriteLine("Usage: sysinfo <cpu|win|net|host|user>"); p = Console.ReadLine();
This work but I see message please any key to countinue after when I write one options ..
Glad it works! Your program ended, this exactly the expected behavior according to your code
@ВалерияБлаговест If this answer or any other one solved your issue, please mark it as accepted
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.

2 Comments

How Can to fix the code to read from the console and get info ?
string input = Console.ReadLine();
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.