-10

パラメータのように .txt ファイルを開くことができる C# のコンソール アプリケーションが必要です。ルートから .txt ファイルを開く方法しか知りません。

var text = File.ReadAllText(@"Input.txt");
Console.WriteLine(text);
4

3 に答える 3

2

出発点。次に、ファイルの内容をどうするかはあなた次第です

using System.IO;    // <- required for File and StreamReader classes

static void Main(string[] args)
{
    if(args != null && args.Length > 0)
    {
        if(File.Exists(args[0]))
        {
            using(StreamReader sr = new StreamReader(args[0]))
            {
                string line = sr.ReadLine();
                ........
            }
        }
    }
}

上記のアプローチでは、一度に 1 行を読み取って最小限の量のテキストを処理しますが、ファイル サイズが一定でない場合は、StreamReader オブジェクトを使用せずに使用できます。

        if(File.Exists(args[0]))
        {
            string[] lines = File.ReadAllLines(args[0]);
            foreach(string line in lines)
            {
                 ... process the current line
            }
        }
于 2013-05-23T09:28:05.697 に答える
1
void Main(string[] args)
{    
  if (args != null && args.Length > 0)
  {
   //Check file exists
   if (File.Exists(args[0])
   {
    string Text = File.ReadAllText(args[0]);
   }

  }    
}
于 2013-05-23T09:28:34.057 に答える
0

ここに基本的なコンソールアプリケーションがあります:

class Program
{
    static void Main(string[] args)
    {
        //Your code here
    }
}

メソッド Main のパラメーター args が必要です。コンソールからプログラムを起動するときに、プログラム名を入力し、その横にパラメーター (ここでは txt ファイルへのパス) を入力します。 args によると、最初のパラメーターは args[0] です。

お役に立てば幸いです

于 2013-05-23T09:30:04.703 に答える