参考:https://www.cnblogs.com/Jayesslee/p/9418797.html
路径中使用/ 和 \都可以,不过Windows下两种都可以,而unix中只能用/
而且由于在ASCII中字符 \ 是转义字符,所以要表示 \ 则要多加一个,所以是 \; 而 / 则不需多添加。
例子:
//strPath = "D:\\OSA\\BK\\20200825-095420-bk.csv"//调试代码中
string strPath = @"D:\OSA\BK\20200825-095420-bk.csv";
int nIndex = strPath.LastIndexOf('\\');//=9
int nDex = strPath.LastIndexOf("/");//=-1
对于反斜杠
//strPath = "D:\\\\OSA\\\\BK\\\\20200825-095420-bk.csv"
string strPath = @"D:\\OSA\\BK\\20200825-095420-bk.csv";
int nIndex = strPath.LastIndexOf('\\');
int nDex = strPath.LastIndexOf("/");
而对于正斜杠
//strPath = "D:/OSA/BK/20200825-095420-bk.csv" 调试代码中
string strPath = @"D:/OSA/BK/20200825-095420-bk.csv";
int nIndex = strPath.LastIndexOf('\\');//=-1
int nDex = strPath.LastIndexOf("/");//=9
下面测试成功:
string strPath = @"D:/OSA/CK/20200825-095420-bk.csv";
int nIndex = strPath.LastIndexOf('/');
string strDir = strPath.Substring(0, nIndex);
if(!Directory.Exists(strDir))
{
Directory.CreateDirectory(strDir);
}
对于路径加@
正斜杠/ 加@ 没有作用
反斜杠\ 加@ 会使 一个\ 变成 两个\
对于 strPath.LastIndexOf(’\’); 中其实 是查找的字符 \
因为 \为转义字符,需要在加一个\才能表示 是\
对于路径
“E:\wokspace\excercise\C#\testchart\testchart\image.png” 不加@时,提示:无法识别的转义序列。
正确做法:需要添加@