Working with paths

Concatenating paths shouldn't be done just using string concatenation, because the directory separator may differ on various platforms. This snippet shows a simple custom operator for working with paths.

Copy Source
Copy Link
Tools:
1: open System.IO
2: 
3: /// Custom operator for combining paths
4: let (+/) path1 path2 = Path.Combine(path1, path2)
5: 
6: // Root path obtained from some function
7: let root = (...)
8: // Create path for a specific file
9: let file = root +/ "Subdirectory" +/ "Test.fsx"
namespace System
namespace System.IO
val path1 : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
val path2 : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
type Path =
  class
    static val DirectorySeparatorChar : char
    static val AltDirectorySeparatorChar : char
    static val VolumeSeparatorChar : char
    static val InvalidPathChars : char []
    static val PathSeparator : char
    static member ChangeExtension : string * string -> string
    static member Combine : string [] -> string
    static member Combine : string * string -> string
    static member Combine : string * string * string -> string
    static member Combine : string * string * string * string -> string
    static member GetDirectoryName : string -> string
    static member GetExtension : string -> string
    static member GetFileName : string -> string
    static member GetFileNameWithoutExtension : string -> string
    static member GetFullPath : string -> string
    static member GetInvalidFileNameChars : unit -> char []
    static member GetInvalidPathChars : unit -> char []
    static member GetPathRoot : string -> string
    static member GetRandomFileName : unit -> string
    static member GetTempFileName : unit -> string
    static member GetTempPath : unit -> string
    static member HasExtension : string -> bool
    static member IsPathRooted : string -> bool
  end

Full name: System.IO.Path
Multiple overloads
Path.Combine(paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
val root : string

Full name: Test.root

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
"C:\\test"
val file : string

Full name: Test.file

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>

More information

Link: http://fssnip.net/1g
Posted: 3 years ago
Author: Tomas Petricek (website)
Tags: path, custom operator