如何传递打开文件的参数C#

how to pass parameters for opening files C#

我是 c# 的新手,我在尝试理解这一点时遇到了问题。如何将参数传递给(或者我倾向于混淆术语)主要功能以读取文本文件?我有一个 python 函数可以更好地显示我想做的事情。

def readFile(filename):
    data = []
    source = file(filename)
    for line in source:
        words = line.split()
        for word in words:
            data.append(int(word))
    source.close()
    return data

我对如何在 C# 中打开文件有基本的了解,但在网上搜索我找不到任何可以帮助我做我想做的事情或至少帮助翻译的东西。这是我的基本理解:

using System;
using System.Text;
using System.IO;

public class Readingfiles {


public static void Main()
{
    StreamReader src = new StreamReader("Data.txt");

    while(!src.EndOfStream)
    {
        string line = src.ReadLine();
        Console.WriteLine(line);
    }

  }
}

请帮助,如果有帮助,我正在使用 sublime text 并通过 mcs/mono 在终端上编译。

您的 main():

应该有输入参数 args
static void Main(string[] args)

举个例子:

class Program {
    static void Main(string[] args) {
        Console.WriteLine("This is the program");
        if (args == null || args.Length == 0) {
            Console.WriteLine("This has no argument");
            Console.ReadKey();
            return;
        }
        Console.WriteLine("This has {0} arguments, the arguments are: ", args.Length);
        for (int i = 0; i < args.Length; ++i)
            Console.WriteLine(args[i]);
        Console.ReadKey();
    }
}

程序将显示您是否使用参数调用 .exe

C:\Release> ConsoleApplication.exe //has no argument
C:\Release> ConsoleApplication.exe Data.txt //has one argument
C:\Release> ConsoleApplication.exe CallWith Data.txt //has two arguments
C:\Release> ConsoleApplication.exe "CallWith Data.txt" //has ONE arguments

在您的情况下,参数可能只是一个字符串,因此,将输入检查放在 args[0] 上就足够了:

public static void Main(string[] args)
{
    if (args == null || args.Length == 0) {
        Console.WriteLine("Error: please specify the file to read!");
        Console.ReadKey();
        return;
    }

    try {

        StreamReader src = new StreamReader(args[0]);

        while(!src.EndOfStream)
        {
            string line = src.ReadLine();
            Console.WriteLine(line);
        }
    } catch (Exception ex) {
        Console.WriteLine("Error while reading the file! " + ex.ToString());
    }

    Console.ReadKey();    
}

编辑:

您的最终解决方案应如下所示(只需将 Main 块放入正确的命名空间和 class)并且您应该使用所有 usings:

using System;
using System.IO;

namespace ConsoleApplication2 {
    class Program {
        public static void Main(string[] args) {
            if (args == null || args.Length == 0) {
                Console.WriteLine("Error: please specify the file to read!");
                Console.ReadKey();
                return;
            }

            try {

                StreamReader src = new StreamReader(args[0]);

                while (!src.EndOfStream) {
                    string line = src.ReadLine();
                    Console.WriteLine(line);
                }
            } catch (Exception ex) {
                Console.WriteLine("Error while reading the file! " + ex.ToString());
            }

            Console.ReadKey();
        }
    }
}

您可以使用

从程序的任何位置访问命令行参数
string[] args = Environment.GetCommandLineArgs();

此外,读取文件中所有行的最简单方法是

if (args.Length > 1)    //the 1st is always the path to app
    string[] lines = System.IO.File.ReadAllLines(ards[1]);