如何传递打开文件的参数C# [英] how to pass parameters for opening files C#
问题描述
我对 c# 非常陌生,我在尝试理解这一点时遇到了问题.如何将参数传递给(或从我倾向于混淆术语)主函数以读取文本文件?我有一个 python 函数,可以更好地显示我想要做什么.
I am very new to c# and Im having issues trying to understanding this. How to pass a parameter to (or from I tend to confuse the terminology) the main function in order to read a text file? I have a python function that better shows what I want to do.
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# 中打开文件有一个基本的了解,但是在网上搜索我找不到任何可以帮助我做我想做的事情或至少帮助翻译的东西.这是我的基本理解:
I have a basic understanding of how to open files in c# but scouring the web I can't find anything that can help me do what I want or at least help translate. here's my basic understanding:
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 在终端上编译.
Please help, If it helps, Im using sublime text and compiling on terminal through mcs/mono.
推荐答案
你的 main()
应该有输入参数 args
:
You should have input argument args
for your main()
:
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
:
The program will show you if you call the .exe
with argument or not:
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]
上就足够了:
In your case, likely the argument is just a single string, thus, putting the input checking on args[0]
will be enough:
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 块置于正确的命名空间和类中)并且您应使用所有 usings
:
Your final solution should look like this (simply put the Main block to the correct namespace and class) and you should use all the 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();
}
}
}
这篇关于如何传递打开文件的参数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!