From 3fa62e313b6058ff5d771dca0841877aa3ef2545 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 15 Nov 2022 16:45:20 +0100 Subject: [PATCH] Add File.writeAll* variants that create parent directories --- .../Prelude/FSLibExtensions.fs | 91 +++++++++++++++++-- 1 file changed, 85 insertions(+), 6 deletions(-) diff --git a/src/Aardvark.Base.FSharp/Prelude/FSLibExtensions.fs b/src/Aardvark.Base.FSharp/Prelude/FSLibExtensions.fs index 80413f34..2ab01c7b 100644 --- a/src/Aardvark.Base.FSharp/Prelude/FSLibExtensions.fs +++ b/src/Aardvark.Base.FSharp/Prelude/FSLibExtensions.fs @@ -529,13 +529,92 @@ module Path = module File = open System.IO - let writeAllLines p lines = File.WriteAllLines (p,lines) - let writeAllText p text = File.WriteAllText (p,text) - let writeAllBytes p bytes = File.WriteAllBytes (p,bytes) + /// + /// Creates the parent directory of the given file path, if it does not exist. + /// + /// The path of the file, whose parent directory is to be created. + let createParentDirectory (path : string) = + let info = FileInfo(path) + if not info.Directory.Exists then + info.Directory.Create() + + /// + /// Creates a new file, writes the specified string array to the file, and then closes the file. + /// + /// The file to write to. + /// The lines to write to the file. + let writeAllLines (path : string) (lines : string[]) = + File.WriteAllLines(path, lines) + + /// + /// Creates a new file, writes the specified string array to the file, and then closes the file. + /// If the parent directory does not exist, it is created first. + /// + /// The file to write to. + /// The lines to write to the file. + let writeAllLinesSafe (path : string) (lines : string[]) = + createParentDirectory path + File.WriteAllLines(path, lines) + + /// + /// Creates a new file, writes the specified string to the file, and then closes the file. + /// + /// The file to write to. + /// The string to write to the file. + let writeAllText (path : string) (text : string) = + File.WriteAllText(path, text) + + /// + /// Creates a new file, writes the specified string to the file, and then closes the file. + /// If the parent directory does not exist, it is created first. + /// + /// The file to write to. + /// The string to write to the file. + let writeAllTextSafe (path : string) (text : string) = + createParentDirectory path + File.WriteAllText(path, text) - let readAllLines p = File.ReadAllLines p - let readAllText p = File.ReadAllText p - let readAllBytes p = File.ReadAllBytes p + /// + /// Creates a new file, writes the specified byte array to the file, and then closes the file. + /// + /// The file to write to. + /// The bytes to write to the file. + let writeAllBytes (path : string) (bytes : uint8[]) = + File.WriteAllBytes(path, bytes) + + /// + /// Creates a new file, writes the specified byte array to the file, and then closes the file. + /// If the parent directory does not exist, it is created first. + /// + /// The file to write to. + /// The bytes to write to the file. + let writeAllBytesSafe (path : string) (bytes : uint8[]) = + createParentDirectory path + File.WriteAllBytes(path, bytes) + + /// + /// Opens a text file, reads all lines of the file into a string array, and then closes the file. + /// + /// The file to open for reading. + /// A string array containing all lines of the file. + let readAllLines (path : string) = + File.ReadAllLines path + + /// + /// Opens a text file, reads all the text in the file into a string, and then closes the file. + /// + /// The file to open for reading. + /// A string containing all the text in the file. + let readAllText (path : string) = + File.ReadAllText path + + /// + /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file. + /// + /// The file to open for reading. + /// A byte array containing the contents of the file. + let readAllBytes (path : string) = + File.ReadAllBytes path [] module NativeUtilities =