FSharp.Management


The CommonFolders module

This tutorial shows the use of the CommonFolders module. It simplifies runtime access to various common folder locations on the system, including folders related to the current user, the shared user folders, system wide folders, and folders specific to the currently executing process.

This is particularly useful when combined with the RelativePath type provider for working with project data and deployed. For example, the following copies a file from the project's doc\contents to the user's roaming application data folder.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
// Reference the type provider dll
#r "FSharp.Management.dll"
open FSharp.Management
open System.IO

// Get the currently executing user's application data folder
let userAppData = CommonFolders.GetUser UserPath.RoamingApplicationData

// Let the type provider do it's work - grab the docs subfolder
type Docs = RelativePath<"docs">

// now you have typed access to your filesystem
let currentFile = Docs.content.CommonFolders.fsx

currentFile
val it : string = "content\CommonFolders.fsx"

// Get the location of the installed executable
let exePath = CommonFolders.GetApplication ApplicationPath.EntryPointLocation

// Build the source and target path - This assumes the exe is in bin\Release or similar
let sourcePath = Path.Combine(exePath, "..\\..", currentFile)
let targetPath = Path.Combine(userAppData, "SomeCompany\\SomeProgram", currentFile)

// Make sure it exists on the system
Directory.CreateDirectory(Path.GetDirectoryName(targetPath))
val it : System.IO.DirectoryInfo = 
  content {Attributes = Directory; 
  ... 

// Copy the source to the target location
File.Copy(sourcePath, targetPath)
val it : unit = () 

// File is now in the appropriate user's [Application data]\SomeCompany\SomeProgram\content\CommonFolders.fsx

The CommonFolders module supports pulling data from various locations, based on any provided UserPath, SharedPath, SystemPath, or ApplicationPath. Note that many of these paths are wrappers around Environment.GetFolderPath, though a few pull from other APIs.

Some examples of getting common folders include:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
// Get the currently executing user's application data folder, both roaming and local profile
let userAppData = CommonFolders.GetUser UserPath.RoamingApplicationData
let userLocalAppData = CommonFolders.GetUser UserPath.LocalApplicationData

// Get the user's Desktop
let userDesktop = CommonFolders.GetUser UserPath.Desktop

// Get user's pictures
let userDesktop = CommonFolders.GetUser UserPath.Pictures

// Get the Windows install folder:
let windows = CommonFolders.GetSystem SystemPath.Windows

// Get the temp folder
let windows = CommonFolders.GetSystem SystemPath.Temp

// Get the shared documents folder
let sharedAppData = CommonFolders.GetSharedUser SharedPath.Documents
namespace FSharp
namespace FSharp.Management
namespace System
namespace System.IO
val userAppData : string

Full name: CommonFolders.userAppData
module CommonFolders

from FSharp.Management
val GetUser : path:UserPath -> string

Full name: FSharp.Management.CommonFolders.GetUser
type UserPath =
  | RoamingApplicationData = 26
  | LocalApplicationData = 28
  | Desktop = 16
  | Documents = 5
  | Pictures = 39
  | Videos = 14
  | Favorites = 6
  | Startup = 7

Full name: FSharp.Management.PathIdentifiers.UserPath
UserPath.RoamingApplicationData: UserPath = 26
type Docs = obj

Full name: CommonFolders.Docs
type RelativePath

Full name: FSharp.Management.RelativePath
val currentFile : string

Full name: CommonFolders.currentFile
val exePath : string

Full name: CommonFolders.exePath
val GetApplication : appPath:ApplicationPath -> string

Full name: FSharp.Management.CommonFolders.GetApplication
type ApplicationPath =
  | EntryPointLocation = 0
  | EntryPointShadowCopiedLocation = 1
  | FSharpManagementLocation = 2
  | FSharpManagementShadowCopiedLocation = 3

Full name: FSharp.Management.PathIdentifiers.ApplicationPath
ApplicationPath.EntryPointLocation: ApplicationPath = 0
val sourcePath : string

Full name: CommonFolders.sourcePath
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : params paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.Combine(params 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 targetPath : string

Full name: CommonFolders.targetPath
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
Directory.CreateDirectory(path: string) : DirectoryInfo
Directory.CreateDirectory(path: string, directorySecurity: System.Security.AccessControl.DirectorySecurity) : DirectoryInfo
Path.GetDirectoryName(path: string) : string
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.Copy(sourceFileName: string, destFileName: string) : unit
File.Copy(sourceFileName: string, destFileName: string, overwrite: bool) : unit
val userLocalAppData : string

Full name: CommonFolders.userLocalAppData
UserPath.LocalApplicationData: UserPath = 28
val userDesktop : string

Full name: CommonFolders.userDesktop
UserPath.Desktop: UserPath = 16
UserPath.Pictures: UserPath = 39
val windows : string

Full name: CommonFolders.windows
val GetSystem : path:SystemPath -> string

Full name: FSharp.Management.CommonFolders.GetSystem
type SystemPath =
  | Windows = 36
  | SystemX86 = 41
  | System = 37
  | Temp = -1

Full name: FSharp.Management.PathIdentifiers.SystemPath
SystemPath.Windows: SystemPath = 36
SystemPath.Temp: SystemPath = -1
val sharedAppData : string

Full name: CommonFolders.sharedAppData
val GetSharedUser : path:SharedPath -> string

Full name: FSharp.Management.CommonFolders.GetSharedUser
type SharedPath =
  | ApplicationData = 35
  | Documents = 46
  | Desktop = 25
  | Music = 53
  | Pictures = 54
  | Video = 55

Full name: FSharp.Management.PathIdentifiers.SharedPath
SharedPath.Documents: SharedPath = 46
Fork me on GitHub