控制台应用程序如何通过在cmd中输入操作来运行函数

时间:2014-02-06 13:00:14

标签: c# asp.net command console-application

在VS 2010的C#控制台应用程序中,我有......

static void Main(string[] args)
    {
        Console.WriteLine("Welcome to c#");

        Console.WriteLine(DownloadPOS());

当我点击播放时打开命令提示符,显示“欢迎使用c#”并运行“DownloadPOS”功能。

我想删除Console.WriteLine(DownloadPOS());,运行程序并且必须在命令行中手动输入文本'DownloadPOS()'。我怎样才能做到这一点?我尝试打开cmd并导航到项目。?

我也尝试右键单击项目/ properties / debug并在命令行参数中输入DownloadPOS()并在debug中运行项目。这应该运行吗?

4 个答案:

答案 0 :(得分:2)

你想做这样的事情:

static void Main(string[] args)
{
    Console.WriteLine("Welcome to c#");
    Console.WriteLine("Please select an option.\n");
    Console.WriteLine("Select D for DownLoadPos(), U for UpLoadPos() ...\n");

    string input = Console.ReadLine();

    switch(input)
    {
          case "D": 
          { 
               DownloadPOS();
               break;
          }
          case "U": 
          { 
               //UpLoadPOS(); // This is not a real method. Just for explanation
               break;
          }
          default:
          {
               Console.WriteLine("You have not selected an option.\n");
               Console.WriteLine("The program will now exit. \n");                   
               System.Threading.Thread.Sleep(1000);
               break;
          }
    }
}

答案 1 :(得分:1)

static void Main(string[] args)
{
    Console.WriteLine("Welcome to c#");

    string input = Console.ReadLine();

    if(input.ToUpper() == "DOWNLOADPOS")
       DownloadPOS();
}

答案 2 :(得分:1)

Console.WriteLine("Welcome to c#");

    string input = Console.ReadLine();

    if(input.ToUpper() == "DOWNLOADPOS")
       DownloadPOS();
    Console.ReadLine();

答案 3 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Welcome to c#");

        // Example 1
        if (args.Count() > 0 && args[0].ToUpper().Equals("DOWNLOADPOS1"))
        {
            Console.WriteLine("Called via Arguments");
            DownloadPOS();
        }
        // Example 2
        else if ("DOWNLOADPOS2" == Console.ReadLine().ToUpper())
        {
            Console.WriteLine("Called via user input");
            DownloadPOS();
        }

        // To stop command prompt disappearing
        Console.ReadLine();
    }

    private static void DownloadPOS()
    {
        Console.WriteLine("Hello from DownloadPOS");
    }
}

如果您将DOWNLOADPOS1作为参数,程序将调用您的方法。输出将是;

欢迎来到c#
通过参数调用
您好,来自DownloadPOS

如果删除命令行参数然后运行程序,它将等待您的输入。输入" DOWNLOADPOS2"并且它将执行您的方法。命令提示符就像;

欢迎来到c#
DOWNLOADPOS2
通过用户输入进行调用
您好,来自DownloadPOS