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


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

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

    解决方案:

    采用:Path.Combine Path.DirectorySeparatorChar

     例如:

    FileStream f = File.Create(@"E:DemoSecurityDemoSecurityDemoMyFile.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”这样的路径最为有用。

  • 相关阅读:
    (九)排序——选择
    (八)排序——冒泡
    (七)排序算法
    (六)递归
    (五)栈
    (三)队列
    (二)稀疏数组
    Nessus号称是世界上最流行的漏洞扫描程序
    meterpreter ???
    IIS网站权限设置原则
  • 原文地址:https://www.cnblogs.com/duanbiflying/p/7662079.html
Copyright © 2020-2023  润新知