/*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) )
|/ \| ) ) _((_
|| (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n
|\ /| _____)) | ! ] U
\.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace WanzyeeStudio{
///
/// Include some convenient methods to extend IO operation.
///
public static class IoUtility{
///
/// Determine if the path can be used to create a file or directory.
///
///
///
/// Optional to throw an exception message or just return false if invalid.
/// A legal path might not be in good format, e.g., "C:dir\ //file" or "/\pc\share\\new.txt".
/// But it's safe to pass to Directory or FileInfo to create.
/// Path in situations below is invalid, even dangerous:
/// 1. Nothing but empty or white-spaces, nowhere to go.
/// 2. Starts with 3 slashes, this causes crash while system looking for parent directories.
/// 3. Includes invalid chars, can't name a file.
/// 4. A name in path starts or ends with space, we can't get the created file, even delete.
///
///
/// true if is creatable; otherwise, false.
/// Path.
/// Flag to throw an exception or return false.
///
public static bool CheckCreatable(string path, bool exception = false){
try{
if(null == path || "" == path.Trim())
throw new Exception("Path cannot be null, empty, or white-spaces only.");
if(Regex.IsMatch(path, @"\A[\\/]{3}"))
throw new Exception("Path cannot start with 3 slashes.");
var _c = Regex.IsMatch(path, @"\A\w:") ? path[0] + path.Substring(2) : path;
_c = Regex.Replace(_c, @"[\\/]", "");
if(-1 != _c.IndexOfAny(Path.GetInvalidFileNameChars()))
throw new Exception("Illegal characters in path.");
if(path.Split('\\', '/').Any(_v => Regex.IsMatch(_v, @"(^\s+\S|\S\s+$)")))
throw new Exception("Directory or file name cannot start or end with white-space.");
return true;
}catch(Exception e){
if(!exception) return false;
throw new ArgumentException(e.Message, "path");
}
}
///
/// Try to delete a file or directory at the specified path.
///
///
///
/// This doesn't work in Web Player.
/// Note, the operation is permanently and irreversibly.
/// Optional to trace and delete ancestor directories if became empty.
///
///
/// Path.
/// If set to true delete ancestor directories if empty.
///
public static void Delete(string path, bool ancestor = false){
#if UNITY_WEBPLAYER
throw new NotSupportedException("'Delete' is not supported in Web Player.");
#else
try{
var _f = new FileInfo(path);
if(_f.Exists) _f.Delete();
var _d = new DirectoryInfo(path);
if(_d.Exists && null != _d.Parent) _d.Delete(true);
if(!ancestor) return;
for(var p = _d.Parent; null != p; p = p.Parent){
if(p.Exists && null != p.Parent) p.Delete(false);
}
}catch{}
#endif
}
}
}