从Visual Studio中的立即窗口:
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"
Run Code Online (Sandbox Code Playgroud)
它们似乎都应该是一样的.
旧的FileSystemObject.BuildPath()没有以这种方式工作......
我有一个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());
}
Run Code Online (Sandbox Code Playgroud)
异常错误:
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 …Run Code Online (Sandbox Code Playgroud)