zoukankan      html  css  js  c++  java
  • 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”这样的路径最为有用。

  • 相关阅读:
    量化平台的发展转
    jmeter全面总结8jmeter实战
    月见笔谈【一】——关于悲剧
    为什么要不断接触和学习新技术之我见
    WPF后台动态调用样式文件
    WPF后台动态添加TabItem并设置样式
    SQL查询SQLSERVER数据库中的临时表结构脚本
    防抖功能的实现
    项目中自定义进度条的实现
    vue3 请求响应拦截
  • 原文地址:https://www.cnblogs.com/duanbiflying/p/7662079.html
Copyright © 2011-2022 走看看