打开文本文件作为命令行参数传递 [英] Opening a text file is passed as a command line parameter

查看:373
本文介绍了打开文本文件作为命令行参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C#中的控制台应用程序,可以打开一个.txt文件像参数。
我只知道如何从根打开一个.txt文件。

I need Console Application in C# which may open a .txt file like a parameter. I know only how to open a .txt file from root.

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


推荐答案

那么你想对文件内容做什么取决于你

A starting point. Then what you want to do with the contents of the file is up to you

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();
                ........
            }
        }
    }
}

上述方法每次读取一行以处理最少量的文本,但是,如果文件大小不是音乐会,您可以避免使用StreamReader对象并使用

the above approach reads one line at a time to process the minimal quantity of text, however, if the file size is not a concert you could avoid the StreamReader object and use

        if(File.Exists(args[0]))
        {
            string[] lines = File.ReadAllLines(args[0]);
            foreach(string line in lines)
            {
                 ... process the current line
            }
        }

这篇关于打开文本文件作为命令行参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆