【发布时间】:2014-05-28 14:19:22
【问题描述】:
这可能是我忽略的东西,但在运行我的程序后,我不断返回:
“ListIT 找不到文件”
这是我的代码:
public static void Main(string[] args)
{
try {
int ctr = 0;
if (args.Length <= 0)
{
Console.WriteLine("Format: ListIT filename");
return;
}
else
{
FileStream f = new FileStream(args[0], FileMode.Open);
try
{
StreamReader t = new StreamReader(f);
string line;
while((line = t.ReadLine()) != null)
{
ctr++;
Console.WriteLine("{0}: {1}", ctr, line);
}
f.Close();
}
finally { f.Close(); }
}
}
catch(System.IO.FileNotFoundException)
{
Console.WriteLine ("ListIT could not find the file ", args[0]);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}\n\n", e);
}
}
这是我在命令行中的输入:
csc.exe ex47_1.exe [回车]
ex47_1.exe listit ex47_1.cs [Enter]
有什么建议吗?我对 C# 还很陌生。
Edt:在过去的 4 年里,我一直在自学编程,这是我第一次使用 Sam 的自学书。我没有意识到所有示例中有多少错误。感谢您的帮助,但这教会了我不要完全依赖来源来确保一切正确。
【问题讨论】:
-
呃……你指定的文件在当前目录下找不到怎么办?
-
将
catch(System.IO.FileNotFoundException)更改为catch(System.IO.FileNotFoundException e)并告诉我们e的值,而不是您的自定义错误消息 -
使用调试器,卢克!
-
@user3101180 所以对你来说,不使用 IDE 而是在 Stack Overflow 上发布问题会更快?
-
@UweKeim 有道理。通过一些打印语句,您可以解决这个问题。看起来好像您正试图打开
args[0],据我所知,args[0]是listit。我想你想打开args[1]
标签: c# file-io command-line filestream streamreader