我使用此代码来查找调试目录
public string str_directory = Environment.CurrentDirectory.ToString();
Run Code Online (Sandbox Code Playgroud)
"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj\\bin\\Debug"
如何找到如下所示的父文件夹?
"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj"
根据有关Path.Combine方法的官方文档:https://msdn.microsoft.com/en-us/library/fyy7a5kt(v = vs.110).aspx
备注
如果path1不是驱动器引用(即"C:"或"D:")并且不以DirectorySeparatorChar,AltDirectorySeparatorChar或VolumeSeparatorChar中定义的有效分隔符结束,则在连接之前会将DirectorySeparatorChar附加到path1.
这意味着它不会\在驱动器号之后添加,所以这段代码:
var path1 = @"c:";
var path2 = @"file.txt";
Path.Combine(path1, path2);
Run Code Online (Sandbox Code Playgroud)
将产生C:file.txt哪些不会强制指向file.txt放入的文件c:.
这背后的原因是什么?
所以,当我跑
TPath.Combine('c:', 'myfile.txt');
Run Code Online (Sandbox Code Playgroud)
在Delphi XE2中我得到'C:myfile.txt'作为回报.这不是我所期望的,它不是Windows中的有效路径.我希望TPath.Combine能够调用windows API(http://msdn.microsoft.com/en-us/library/fyy7a5kt%28v=vs.110%29.aspx)或者具有相同的行为API.
有什么我做错了吗?我可以"修复"TPath.Combine的行为吗?或者我是否必须在我的代码中搜索所有用途并将其替换为字符串连接,并在其间使用"\"?