Table of Contents

Class FileSystemSupport

Namespace
Wyrowski.Infrastructure.Support
Assembly
Wyrowski.Infrastructure.dll

\unstable Static class to support access to the file system and handling of file names.

public static class FileSystemSupport
Inheritance
FileSystemSupport
Inherited Members

Properties

ApplicationSpecificTemporaryDirectory

Gets the application and version specific temporary directory as sub-directory of TemporaryDirectory.

public static string ApplicationSpecificTemporaryDirectory { get; }

Property Value

string

ClipboardDirectory

Temporary folder where files are stored which are passed around like with the Windows™ Clipboard.

public static string ClipboardDirectory { get; }

Property Value

string

ExecutableDirectory

Gets the directory where the main executable of the application is located.

public static string ExecutableDirectory { get; }

Property Value

string

ProgramDataDirectoryCurrentVersion

Gets and sets the directory where data from LightTrans or Wyrowski Photonics is stored for the current user. For example catalogs, detector add-ons, …. Every user has its own program data directory because "standard users" (without administrator privileges) cannot update into the normal program data directory.

public static string ProgramDataDirectoryCurrentVersion { get; }

Property Value

string

TemporaryDirectory

Temporary directory to save temporary data during calculations.

public static string TemporaryDirectory { get; set; }

Property Value

string

UserDataDirectory

[SI] Directory containing all user-defined catalogs and log files. \static{}

public static string UserDataDirectory { get; set; }

Property Value

string

Methods

AddExtension(string, string)

Adds an extension to the given file name.

public static string AddExtension(string fileName, string extension)

Parameters

fileName string

The file name.

extension string

The extension.

Returns

string

The file name with the added extension. If the extension does not contain a leading period (.), a period is added. In contrast to Path.ChangeExtension, "filename.3" + ".ext" becomes "filename.3.ext", not "filename.ext". If the file name already ends with the given extension, it is not added anew.

CopyDirectory(string, string, bool, bool)

Copies all files from the given source directory to the destination directory.

public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool overwrite)

Parameters

sourceDir string

The source directory.

destinationDir string

The destination directory.

recursive bool

If set to true, also the content of all sub-directories is copied.

overwrite bool

If set to true existing files are overwritten, even if they are read-only.

Exceptions

DirectoryNotFoundException

Source directory not found.

CreateTemporaryFileName(string, string)

Creates a unique random file name with the given path and extension.

public static string CreateTemporaryFileName(string path, string extension)

Parameters

path string

Path in which uniqueness with the file name is ensured.

extension string

The file extension (e.g. ".mtr") appended to the file name.

Returns

string

The random file name including the given path and the extension.

CreateTemporarySubDirectory()

Creates a new sub-directory in the version specific ApplicationSpecificTemporaryDirectory.

public static string CreateTemporarySubDirectory()

Returns

string

Full path of the newly created sub-directory for temporary files.

DeleteFileIfEmpty(string?)

Deletes the given file if it is empty (i.e. has a length of zero bytes).

public static void DeleteFileIfEmpty(string? filePath)

Parameters

filePath string

The complete path of the file.

DeleteTempDir()

Deletes the main temp dir.

public static void DeleteTempDir()

EnsureThatDirectoryExists(string, bool)

Ensures that the given directory exists.

public static void EnsureThatDirectoryExists(string path, bool containsFileName = false)

Parameters

path string

The path to check.

containsFileName bool

If this optional parameter is set to true it is assumed that the path contains a file name (which is removed prior to the check.

GetFilesFromDirectory(string, string, bool, bool)

Wrapper around the GetFiles(string) method with the following changes:
• It is optionally searched recursively in all sub-directories.

• Inaccessible files are ignored (.Net 8 only).
• You can optionally exclude backup files created by the MoveToBackupFile(string, string) method.

public static string[] GetFilesFromDirectory(string directory, string searchPattern = "*", bool excludeBackupFiles = false, bool includeSubDirectories = true)

Parameters

directory string

The relative or absolute path to the directory to search. This string is not case-sensitive.

searchPattern string

Optional search pattern. Only file names containing this pattern are included in the result.

excludeBackupFiles bool

If this optional parameter is set to true backup files created by the MoveToBackupFile(string, string) or the GetBinaryBackupFileName(string) method are excluded.

includeSubDirectories bool

If this optional parameter is set to false, only the given directory is searched. Else also all sub-directories are searched.

Returns

string[]

An array of the full names (including paths) of the found files in the specified directory, or an empty array if no files are found or if the directory does not exist.

GetSpecialFolder(SpecialFolder, bool)

Gets the specified special folder to which company and product name have been added as subdirectories. For example "c:\Users{user}\AppData\Roaming\Wyrowski Photonics GmbH\VirtualLab Fusion" is returned for "Environment.SpecialFolder.ApplicationData".

public static string GetSpecialFolder(Environment.SpecialFolder specialFolder, bool createVersionSpecificSubfolder)

Parameters

specialFolder Environment.SpecialFolder

Enumeration entry specifying the special folder.

createVersionSpecificSubfolder bool

If set to true a version specific sub-folder is created (e.g. "2024.1").

Returns

string

The complete directory path, with company, product name, and optionally version as sub-directories. If anything fails, the temporary path is returned.

IsDirectoryEmpty(string)

Checks whether the given directory is empty.

public static bool IsDirectoryEmpty(string path)

Parameters

path string

The path to the directory.

Returns

bool

True if the specified directory is empty.

Exceptions

DirectoryNotFoundException

Directory does not exist.

MoveToBackupFile(string, string)

Moves a (faulty) file to a backup file, whereas already existing backup files are not overwritten.

public static void MoveToBackupFile(string fileName, string description = "")

Parameters

fileName string

Name of the file.

description string

The description for the error message. Must end with a period. E.g. "File could not be read." If this optional parameter is not specified, no error message is put out.

RecursiveDelete(DirectoryInfo)

Deletes the given directory and all its sub-directories. If there are files in the directory which have the read-only attribute or cannot be deleted because they are still in use or due to lacking access rights, the normal Directory.Delete just throws an exception and many files are not deleted.

public static void RecursiveDelete(DirectoryInfo directoryToDelete)

Parameters

directoryToDelete DirectoryInfo

The directory to delete.