portability flaw : file separator (可移植性缺陷:文件分隔符)

 portability flaw : file separator (可移植性缺陷:文件分隔符)

问题产生原因: 主要是文件地址上采用硬编码导致 如 “\\”

 

解决方案:

采用:Path.Combine Path.DirectorySeparatorChar

 

 例如:

FileStream f = File.Create(@"E:\Demo\SecurityDemo\SecurityDemo\MyFile.txt");

 

可改成

FileStream f1 = File.Create(Path.Combine(
                string.Format(@"E:{0}Demo{0}SecurityDemo{0}SecurityDemo{0}MyFile.txt", Path.DirectorySeparatorChar)));

 

 

参考文档

目录分隔符 

Path.DirectorySeparatorChar 字段

public static readonly char DirectorySeparatorChar

提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。

在该字段中存储的字符不能位于 InvalidPathChars 中。AltDirectorySeparatorChar 和 DirectorySeparatorChar 都适用于在路径字符串中分隔目录级别。

该字段的值在 Unix 上为斜杠(“/”),在 Windows 和 Macintosh 操作系统上为反斜杠(“\”)。

 

替换分隔符

Path.AltDirectorySeparatorChar

public static readonly char AltDirectorySeparatorChar

提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。

在此字段中存储的字符不能位于 InvalidPathChars 中。该字段可被设置为与 DirectorySeparatorChar 相同的值。AltDirectorySeparatorChar 和 DirectorySeparatorChar 都适用于在路径字符串中分隔目录级别。

该字段的值在 Unix 上为反斜杠(“\”),在 Windows 和 Macintosh 操作系统上为斜杠(“/”)。

 

环境变量中路径分隔符

Path.PathSeparator

public static readonly char PathSeparator

用于在环境变量中分隔路径字符串的平台特定的分隔符。在基于 Windows 的桌面平台上,默认情况下该字段的值是分号 (;),但在其他平台上可能会有所不同。

 

卷分隔符

Path.AltDirectorySeparatorChar

public static readonly char VolumeSeparatorChar

提供平台特定的卷分隔符。该字段的值在 Windows 和 Macintosh 上为冒号(“:”),在 Unix 操作系统上为斜杠(“/”)。这对于分析像“c:\windows”或“MacVolume:System Folder”这样的路径最为有用。

 

转载于:https://www.cnblogs.com/duanbiflying/p/7662079.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值