C#中路径表示\ 和 /

参考: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” 不加@时,提示:无法识别的转义序列。

正确做法:需要添加@

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值