【发布时间】:2019-02-28 07:48:20
【问题描述】:
我已经使用 Linux 多年了,由于这个习惯,我添加了 /(斜杠)而不是 \(反斜杠)作为我的 c# 代码的路径。
private const string X86DllPath = "x86/ShaferFilechck.dll"; // should use x86\\ShaferFilechck.dll
private const string X64DllPath = "x64/ShaferFilechck.dll";
[DllImport(X64DllPath, EntryPoint = "NalpLibOpen", CallingConvention = CallingConvention.Cdecl)]
private static extern int NalpLibOpen_64(byte[] xmlParms);
代码有效,但我想知道 c# 在 Windows 上编译和运行时是否将斜杠转换为反斜杠。是否有一些官方文件说明这一点或其他内容。
在某些情况下,在路径中使用 / 可能会导致一些意外行为。
更新:
我知道最终的解决方案是使用它,但我想知道为什么斜线仍然“可以”:
Path.Combine("x86", "ShaferFilechck.dll"); // x86\ShaferFilechck.dll
【问题讨论】:
-
查看this 答案,该答案建议使用
Path.DirectorySeparatorChar以获得更好的可移植性。