我有一个 C# Visual Studio 2008 表单,它需要读取相关文件“character/attacks.txt”的内容 File.Exists() 在我运行它时返回 false,即使我确定我的目录已排序。代码:
try
{
System.IO.StreamReader file = new System.IO.StreamReader("character/attacks.txt");
int counter = 0;
int numberOfLines = 0;
string line;
while ((line = file.ReadLine()) != null) { numberOfLines++; }
string[] attacks = new string[numberOfLines];
while ((line = file.ReadLine()) != null) { attacks[counter] = line; counter++; }
file.Close();
print(attacks.ToString());
}
catch (Exception ex)
{
print("ERROR"); print("Did you edit any files?"); print(ex.ToString());
}
异常错误:
System.IO.FileNotFoundException: Could not find file 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'.
File name: 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path)
at TurnByTurn.Form1.button1_Click(Object sender, EventArgs e) in D:\Users\Andrey\Desktop\C#\TurnByTurn\TurnByTurn\Form1.cs:line 52
我正在从 Python 移植我的代码,并且从来没有遇到过任何麻烦。提前致谢!