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”这样的路径最为有用。

 

posted @ 2017-10-13 17:04  一只大老鼠  阅读(3795)  评论(0编辑  收藏  举报