Crosstales.Common.Util.FileHelper Class Reference

Various helper functions for the file system. More...

Static Public Member Functions

static bool isUnixPath (string path)
 Checks if the given path is from a Unix-device More...
 
static bool isWindowsPath (string path)
 Checks if the given path is from a Windows-device More...
 
static bool isUNCPath (string path)
 Checks if the given path is UNC More...
 
static bool isURL (string path)
 Checks if the given path is an URL More...
 
static string ValidatePath (string path, bool addEndDelimiter=true, bool preserveFile=true, bool removeInvalidChars=true)
 Validates a given path and add missing slash. More...
 
static string ValidateFile (string path, bool removeInvalidChars=true)
 Validates a given file. More...
 
static bool HasPathInvalidChars (string path, bool ignoreNullOrEmpty=true)
 Checks a given path for invalid characters More...
 
static bool HasFileInvalidChars (string file, bool ignoreNullOrEmpty=true)
 Checks a given file for invalid characters More...
 
static string[] GetFilesForName (string path, bool isRecursive=false, params string[] filenames)
 Find files inside a path. More...
 
static string[] GetFiles (string path, bool isRecursive=false, params string[] extensions)
 Find files inside a path. More...
 
static string[] GetDirectories (string path, bool isRecursive=false)
 Find directories inside. More...
 
static string[] GetDrives ()
 Find all logical drives. More...
 
static bool CopyDirectory (string sourceDir, string destDir, bool move=false, bool moveSafe=true)
 Copy or move a directory. More...
 
static bool CopyFile (string sourceFile, string destFile, bool move=false, bool moveSafe=true)
 Copy or move a file. More...
 
static bool MoveDirectory (string sourceDir, string destDir)
 Move a directory. More...
 
static bool MoveFile (string sourceFile, string destFile)
 Move a file. More...
 
static string RenameDirectory (string path, string newName)
 Renames a directory in a path. More...
 
static string RenameFile (string path, string newName)
 Renames a file in a path. More...
 
static bool DeleteFile (string file)
 Delete a file. More...
 
static bool DeleteDirectory (string dir)
 Delete a directory. More...
 
static bool ExistsFile (string file)
 Checks if the directory exists. More...
 
static bool ExistsDirectory (string path)
 Checks if the directory exists. More...
 
static string CreateDirectory (string path, string folderName)
 Creates a directory in a given path. More...
 
static bool CreateDirectory (string path)
 Creates a directory. More...
 
static string CreateFile (string path, string fileName)
 Creates a file in a given path. More...
 
static bool CreateFile (string path)
 Creates a file. More...
 
static bool isDirectory (string path, bool checkForExtensions=true)
 Checks if the path is a directory. More...
 
static bool isFile (string path, bool checkForExtensions=true)
 Checks if the path is a file. More...
 
static bool isRoot (string path)
 Checks if the path is the root. More...
 
static string GetFileName (string path, bool removeInvalidChars=true)
 Returns the file name for the path. More...
 
static string GetCurrentDirectoryName (string path)
 Returns the current directory name for the path. More...
 
static string GetDirectoryName (string path)
 Returns the directory name for the path. More...
 
static long GetFilesize (string path)
 Returns the size of a file. More...
 
static string GetExtension (string path)
 Returns the extension of a file. More...
 
static System.DateTime GetLastModifiedDate (string path)
 Returns the size of a file. More...
 
static string ReadAllText (string sourceFile, System.Text.Encoding encoding=null)
 Reads the text of a file. More...
 
static string[] ReadAllLines (string sourceFile, System.Text.Encoding encoding=null)
 Reads all lines of text from a file. More...
 
static byte[] ReadAllBytes (string sourceFile)
 Reads the bytes of a file. More...
 
static bool WriteAllText (string destFile, string text, System.Text.Encoding encoding=null)
 Writes text to a file. More...
 
static bool WriteAllLines (string destFile, string[] lines, System.Text.Encoding encoding=null)
 Writes all lines of text to a file. More...
 
static bool WriteAllBytes (string destFile, byte[] data)
 Writes bytes to a file. More...
 
static bool ShowPath (string path)
 Shows the location of a path (or file) in OS file explorer. NOTE: only works on standalone platforms More...
 
static bool ShowFile (string file)
 Shows the location of a file (or path) in OS file explorer. NOTE: only works on standalone platforms More...
 
static bool OpenFile (string file)
 Opens a file with the OS default application. NOTE: only works for standalone platforms More...
 
static bool PathHasInvalidChars (string path)
 Checks a given path for invalid characters More...
 
static bool FileHasInvalidChars (string file)
 Checks a given file for invalid characters More...
 
static bool CopyPath (string sourceDir, string destDir, bool move=false)
 Copy or move a directory. More...
 
static bool MovePath (string sourceDir, string destDir)
 Move a directory. More...
 

Static Public Attributes

static string ApplicationDataPath => _applicationDataPath
 Returns the Unity application data path. More...
 
static string ApplicationTempPath => _applicationTempPath
 Returns the Unity application temporary path. More...
 
static string ApplicationPersistentPath => _applicationPersistentPath
 Returns the Unity application persistent path. More...
 
static string TempFile => System.IO.Path.GetTempFileName()
 Returns a temporary file. More...
 
static string TempPath => System.IO.Path.GetTempPath()
 Returns the temporary directory path from the device. More...
 

Properties

static string StreamingAssetsPath [get]
 Returns the path to the the "Streaming Assets". More...
 

Detailed Description

Various helper functions for the file system.

Member Function Documentation

◆ CopyDirectory()

static bool Crosstales.Common.Util.FileHelper.CopyDirectory ( string  sourceDir,
string  destDir,
bool  move = false,
bool  moveSafe = true 
)
static

Copy or move a directory.

Parameters
sourceDirSource directory path
destDirDestination directory path
moveMove directory instead of copy (optional, default: false)
moveSafeMoves a directory in a safe, but slower way (optional, default: true)
Returns
True if the operation was successful

◆ CopyFile()

static bool Crosstales.Common.Util.FileHelper.CopyFile ( string  sourceFile,
string  destFile,
bool  move = false,
bool  moveSafe = true 
)
static

Copy or move a file.

Parameters
sourceFileSource file path
destFileDestination file path
moveMove file instead of copy (optional, default: false)
moveSafeMoves a file in a safe, but slower way (optional, default: true)
Returns
True if the operation was successful

◆ CopyPath()

static bool Crosstales.Common.Util.FileHelper.CopyPath ( string  sourceDir,
string  destDir,
bool  move = false 
)
static

Copy or move a directory.

Parameters
sourceDirSource directory path
destDirDestination directory path
moveMove directory instead of copy (optional, default: false)
Returns
True if the operation was successful

◆ CreateDirectory() [1/2]

static bool Crosstales.Common.Util.FileHelper.CreateDirectory ( string  path)
static

Creates a directory.

Parameters
pathPath to the directory to create
Returns
True if the operation was successful

◆ CreateDirectory() [2/2]

static string Crosstales.Common.Util.FileHelper.CreateDirectory ( string  path,
string  folderName 
)
static

Creates a directory in a given path.

Parameters
pathPath for the directory
folderNameNew folder

◆ CreateFile() [1/2]

static bool Crosstales.Common.Util.FileHelper.CreateFile ( string  path)
static

Creates a file.

Parameters
pathPath to the file to create
Returns
True if the operation was successful

◆ CreateFile() [2/2]

static string Crosstales.Common.Util.FileHelper.CreateFile ( string  path,
string  fileName 
)
static

Creates a file in a given path.

Parameters
pathPath for the file
fileNameNew file

◆ DeleteDirectory()

static bool Crosstales.Common.Util.FileHelper.DeleteDirectory ( string  dir)
static

Delete a directory.

Parameters
dirDirectory to delete
Returns
True if the operation was successful

◆ DeleteFile()

static bool Crosstales.Common.Util.FileHelper.DeleteFile ( string  file)
static

Delete a file.

Parameters
fileFile to delete
Returns
True if the operation was successful

◆ ExistsDirectory()

static bool Crosstales.Common.Util.FileHelper.ExistsDirectory ( string  path)
static

Checks if the directory exists.

Returns
True if the directory exists

◆ ExistsFile()

static bool Crosstales.Common.Util.FileHelper.ExistsFile ( string  file)
static

Checks if the directory exists.

Returns
True if the directory exists

◆ FileHasInvalidChars()

static bool Crosstales.Common.Util.FileHelper.FileHasInvalidChars ( string  file)
static

Checks a given file for invalid characters

Parameters
fileFile to check for invalid characters
Returns
Returns true if the file contains invalid chars, otherwise it's false.

◆ GetCurrentDirectoryName()

static string Crosstales.Common.Util.FileHelper.GetCurrentDirectoryName ( string  path)
static

Returns the current directory name for the path.

Parameters
pathPath to the directory
Returns
Current directory name for the path

◆ GetDirectories()

static string [] Crosstales.Common.Util.FileHelper.GetDirectories ( string  path,
bool  isRecursive = false 
)
static

Find directories inside.

Parameters
pathPath to find the directories
isRecursiveRecursive search (optional, default: false)
Returns
Returns array of the found directories inside the path. Zero length array when an error occured.

◆ GetDirectoryName()

static string Crosstales.Common.Util.FileHelper.GetDirectoryName ( string  path)
static

Returns the directory name for the path.

Parameters
pathPath to the directory
Returns
Directory name for the path

◆ GetDrives()

static string [] Crosstales.Common.Util.FileHelper.GetDrives ( )
static

Find all logical drives.

Returns
Returns array of the found drives. Zero length array when an error occured.

◆ GetExtension()

static string Crosstales.Common.Util.FileHelper.GetExtension ( string  path)
static

Returns the extension of a file.

Parameters
pathPath to the file
Returns
Extension of the file

◆ GetFileName()

static string Crosstales.Common.Util.FileHelper.GetFileName ( string  path,
bool  removeInvalidChars = true 
)
static

Returns the file name for the path.

Parameters
pathPath to the file
removeInvalidCharsRemoves invalid characters in the file name (optional, default: true)
Returns
File name for the path

◆ GetFiles()

static string [] Crosstales.Common.Util.FileHelper.GetFiles ( string  path,
bool  isRecursive = false,
params string[]  extensions 
)
static

Find files inside a path.

Parameters
pathPath to find the files
isRecursiveRecursive search (optional, default: false)
extensionsExtensions for the file search, e.g. "png" (optional)
Returns
Returns array of the found files inside the path (alphabetically ordered). Zero length array when an error occured.

◆ GetFilesForName()

static string [] Crosstales.Common.Util.FileHelper.GetFilesForName ( string  path,
bool  isRecursive = false,
params string[]  filenames 
)
static

Find files inside a path.

Parameters
pathPath to find the files
isRecursiveRecursive search (optional, default: false)
filenamesArray of file names for the file search, e.g. "Image.png" (optional)
Returns
Returns array of the found files inside the path (alphabetically ordered). Zero length array when an error occured.

◆ GetFilesize()

static long Crosstales.Common.Util.FileHelper.GetFilesize ( string  path)
static

Returns the size of a file.

Parameters
pathPath of the file
Returns
Size for the file

◆ GetLastModifiedDate()

static System.DateTime Crosstales.Common.Util.FileHelper.GetLastModifiedDate ( string  path)
static

Returns the size of a file.

Parameters
pathPath to the file
Returns
Size for the file

◆ HasFileInvalidChars()

static bool Crosstales.Common.Util.FileHelper.HasFileInvalidChars ( string  file,
bool  ignoreNullOrEmpty = true 
)
static

Checks a given file for invalid characters

Parameters
fileFile to check for invalid characters
ignoreNullOrEmptyIf set to true, return false for null or empty paths (optional, default: true)
Returns
Returns true if the file contains invalid chars, otherwise it's false.

◆ HasPathInvalidChars()

static bool Crosstales.Common.Util.FileHelper.HasPathInvalidChars ( string  path,
bool  ignoreNullOrEmpty = true 
)
static

Checks a given path for invalid characters

Parameters
pathPath to check for invalid characters
ignoreNullOrEmptyIf set to true, return false for null or empty paths (optional, default: true)
Returns
Returns true if the path contains invalid chars, otherwise it's false.

◆ isDirectory()

static bool Crosstales.Common.Util.FileHelper.isDirectory ( string  path,
bool  checkForExtensions = true 
)
static

Checks if the path is a directory.

Parameters
pathPath to the directory
checkForExtensionsCheck for extensions (optional, default: true)
Returns
True if the path is a directory

◆ isFile()

static bool Crosstales.Common.Util.FileHelper.isFile ( string  path,
bool  checkForExtensions = true 
)
static

Checks if the path is a file.

Parameters
pathPath to the file
checkForExtensionsCheck for extensions (optional, default: true)
Returns
True if the path is a file

◆ isRoot()

static bool Crosstales.Common.Util.FileHelper.isRoot ( string  path)
static

Checks if the path is the root.

Parameters
pathPossible root
Returns
True if the path is the root

◆ isUNCPath()

static bool Crosstales.Common.Util.FileHelper.isUNCPath ( string  path)
static

Checks if the given path is UNC

Parameters
pathPath to check
Returns
True if the given path is UNC

◆ isUnixPath()

static bool Crosstales.Common.Util.FileHelper.isUnixPath ( string  path)
static

Checks if the given path is from a Unix-device

Parameters
pathPath to check
Returns
True if the given path is from a Unix-device

◆ isURL()

static bool Crosstales.Common.Util.FileHelper.isURL ( string  path)
static

Checks if the given path is an URL

Parameters
pathPath to check
Returns
True if the given path is an URL

◆ isWindowsPath()

static bool Crosstales.Common.Util.FileHelper.isWindowsPath ( string  path)
static

Checks if the given path is from a Windows-device

Parameters
pathPath to check
Returns
True if the given path is from a Windows-device

◆ MoveDirectory()

static bool Crosstales.Common.Util.FileHelper.MoveDirectory ( string  sourceDir,
string  destDir 
)
static

Move a directory.

Parameters
sourceDirSource directory path
destDirDestination directory path
Returns
True if the operation was successful

◆ MoveFile()

static bool Crosstales.Common.Util.FileHelper.MoveFile ( string  sourceFile,
string  destFile 
)
static

Move a file.

Parameters
sourceFileSource file path
destFileDestination file path
Returns
True if the operation was successful

◆ MovePath()

static bool Crosstales.Common.Util.FileHelper.MovePath ( string  sourceDir,
string  destDir 
)
static

Move a directory.

Parameters
sourceDirSource directory path
destDirDestination directory path
Returns
True if the operation was successful

◆ OpenFile()

static bool Crosstales.Common.Util.FileHelper.OpenFile ( string  file)
static

Opens a file with the OS default application. NOTE: only works for standalone platforms

Parameters
fileFile path
Returns
True if the operation was successful

◆ PathHasInvalidChars()

static bool Crosstales.Common.Util.FileHelper.PathHasInvalidChars ( string  path)
static

Checks a given path for invalid characters

Parameters
pathPath to check for invalid characters
Returns
Returns true if the path contains invalid chars, otherwise it's false.

◆ ReadAllBytes()

static byte [] Crosstales.Common.Util.FileHelper.ReadAllBytes ( string  sourceFile)
static

Reads the bytes of a file.

Parameters
sourceFileSource file path
Returns
Byte-content of the file

◆ ReadAllLines()

static string [] Crosstales.Common.Util.FileHelper.ReadAllLines ( string  sourceFile,
System.Text.Encoding  encoding = null 
)
static

Reads all lines of text from a file.

Parameters
sourceFileSource file path
encodingEncoding of the text (optional, default: UTF8)
Returns
Array of text lines from the file

◆ ReadAllText()

static string Crosstales.Common.Util.FileHelper.ReadAllText ( string  sourceFile,
System.Text.Encoding  encoding = null 
)
static

Reads the text of a file.

Parameters
sourceFileSource file path
encodingEncoding of the text (optional, default: UTF8)
Returns
Text-content of the file

◆ RenameDirectory()

static string Crosstales.Common.Util.FileHelper.RenameDirectory ( string  path,
string  newName 
)
static

Renames a directory in a path.

Parameters
pathPath to the directory
newNameNew name for the directory
Returns
New path of the directory

◆ RenameFile()

static string Crosstales.Common.Util.FileHelper.RenameFile ( string  path,
string  newName 
)
static

Renames a file in a path.

Parameters
pathPath to the file
newNameNew name for the file
Returns
New path of the file

◆ ShowFile()

static bool Crosstales.Common.Util.FileHelper.ShowFile ( string  file)
static

Shows the location of a file (or path) in OS file explorer. NOTE: only works on standalone platforms

Returns
True if the operation was successful

◆ ShowPath()

static bool Crosstales.Common.Util.FileHelper.ShowPath ( string  path)
static

Shows the location of a path (or file) in OS file explorer. NOTE: only works on standalone platforms

Returns
True if the operation was successful

◆ ValidateFile()

static string Crosstales.Common.Util.FileHelper.ValidateFile ( string  path,
bool  removeInvalidChars = true 
)
static

Validates a given file.

Parameters
pathFile to validate
removeInvalidCharsRemoves invalid characters in the file name (optional, default: true)
Returns
Valid file path

◆ ValidatePath()

static string Crosstales.Common.Util.FileHelper.ValidatePath ( string  path,
bool  addEndDelimiter = true,
bool  preserveFile = true,
bool  removeInvalidChars = true 
)
static

Validates a given path and add missing slash.

Parameters
pathPath to validate
addEndDelimiterAdd delimiter at the end of the path (optional, default: true)
preserveFilePreserves a given file in the path (optional, default: true)
removeInvalidCharsRemoves invalid characters in the path name (optional default: true)
Returns
Valid path

◆ WriteAllBytes()

static bool Crosstales.Common.Util.FileHelper.WriteAllBytes ( string  destFile,
byte[]  data 
)
static

Writes bytes to a file.

Parameters
destFileDestination file path
dataByte-content to write
Returns
True if the operation was successful

◆ WriteAllLines()

static bool Crosstales.Common.Util.FileHelper.WriteAllLines ( string  destFile,
string[]  lines,
System.Text.Encoding  encoding = null 
)
static

Writes all lines of text to a file.

Parameters
destFileDestination file path
linesArray of text lines to write
encodingEncoding of the text (optional, default: UTF8)
Returns
True if the operation was successful

◆ WriteAllText()

static bool Crosstales.Common.Util.FileHelper.WriteAllText ( string  destFile,
string  text,
System.Text.Encoding  encoding = null 
)
static

Writes text to a file.

Parameters
destFileDestination file path
textText-content to write
encodingEncoding of the text (optional, default: UTF8)
Returns
True if the operation was successful

Member Data Documentation

◆ ApplicationDataPath

string Crosstales.Common.Util.FileHelper.ApplicationDataPath => _applicationDataPath
static

Returns the Unity application data path.

Returns
Unity application data path

◆ ApplicationPersistentPath

string Crosstales.Common.Util.FileHelper.ApplicationPersistentPath => _applicationPersistentPath
static

Returns the Unity application persistent path.

Returns
Unity application persistent path

◆ ApplicationTempPath

string Crosstales.Common.Util.FileHelper.ApplicationTempPath => _applicationTempPath
static

Returns the Unity application temporary path.

Returns
Unity application temporary path

◆ TempFile

string Crosstales.Common.Util.FileHelper.TempFile => System.IO.Path.GetTempFileName()
static

Returns a temporary file.

Returns
Temporary file

◆ TempPath

string Crosstales.Common.Util.FileHelper.TempPath => System.IO.Path.GetTempPath()
static

Returns the temporary directory path from the device.

Returns
Temporary directory path of the device

Property Documentation

◆ StreamingAssetsPath

string Crosstales.Common.Util.FileHelper.StreamingAssetsPath
staticget

Returns the path to the the "Streaming Assets".

Returns
The path to the the "Streaming Assets".

The documentation for this class was generated from the following file:
  • C:/Users/slaub/Unity/assets/DJPro/DJPro/Assets/Plugins/crosstales/Common/Scripts/Util/FileHelper.cs