如何在控制台应用程序的命令行中输入文本文件作为输入... C#程序

Ran*_*nam 1 c#

我在控制台应用程序中编写了一个程序.我在命令行中上传文本文件时遇到问题.如果我放置直接路径"string s = File.ReadAllText("E:/Aspdot.txt");" 像这样在编程它的工作正常.但是,我希望在运行时上传或提及路径作为commanline输入.

在这里,我正在放置我的踪迹......任何人都可以建议我怎么做......

class Program
{
 static void Main()
 {
    // 1.
    // Array to store occurances.
    int[] c = new int[(int)char.MaxValue];

    // 2.
    // Read entire text file.
    Console.WriteLine("Please enter your text file path");
    String a = Console.ReadLine();
    //string s = File.ReadAllText("E:/Aspdot.txt");
    string s = File.ReadAllText(a);

    // 3.
    // Iterate over each character.
    foreach (char t in s)
    {
        // Increment table.
        c[(int)t]++;
    }

    // 4.
    // Write all letters found.
    for (int i = 0; i < (int)char.MaxValue; i++)
    {
        if (c[i] > 0 &&
            char.IsLetter((char)i))
        {
            Console.WriteLine("Letter: {0}  Occurances: {1}",
                (char)i,
                c[i]);

        }
    }
    Console.ReadLine();
 }

}
Run Code Online (Sandbox Code Playgroud)

Tim*_*mwi 10

使用命令行参数:

public static void Main(string[] args)
{
    if (args == null || args.Length == 0)
    {
        Console.WriteLine("Please specify a filename as a parameter.");
        return;
    }

    var fileContents = File.ReadAllText(args[0]);

    // ... do something with the file contents
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用程序:

MyProgram MyFile.txt
Run Code Online (Sandbox Code Playgroud)

从STDIN读取文件:

public static void Main()
{
    var fileContents = Console.In.ReadToEnd();

    // ... do something with the file contents
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用程序:

MyProgram < MyFile.txt
Run Code Online (Sandbox Code Playgroud)

要么

type MyFile.txt | MyProgram
Run Code Online (Sandbox Code Playgroud)