using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Aitex.Common.Util
{
public class PathManager
{
///
/// Application directory
///
///
public static string GetAppDir()
{
return string.IsNullOrWhiteSpace(_appPath) ? _appPath = GetAppStartupDirectory() : _appPath;
}
///
/// Application config directory is relative to the directory of the currently running application.
/// end with the "/"
///
///
public static string GetCfgDir()
{
return GetDirectory("Config");
}
///
/// Application's log directory is relative to the directory of the currently running application.
///
///
public static string GetLogDir()
{
return GetDirectory("Logs");
}
///
/// Application's process recipe directory
///
///
public static string GetRecipeDir()
{
return GetDirectory("Recipes");
}
///
/// Application's account file path
///
///
public static string GetAccountFilePath()
{
return GetDirectory("Account");
}
///
/// Application start up directory
///
///
static string GetAppStartupDirectory()
{
var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string dir = Path.GetDirectoryName(startupPath);
return dir;
}
public static string GetDirectory(string directoryPath)
{
var ret = Path.Combine(GetAppDir(), directoryPath);
if (!ret.EndsWith(Path.DirectorySeparatorChar.ToString()))
ret = ret + Path.DirectorySeparatorChar;
if (!Directory.Exists(ret))
Directory.CreateDirectory(ret);
return ret;
}
public static bool IsValidDirectoryPath(string path)
{
if (string.IsNullOrWhiteSpace(path)) return false;
var drive = DriveInfo.GetDrives().FirstOrDefault(p => p.Name == Path.GetPathRoot(path));
if (drive == null || !drive.IsReady) return false;
string pattern = @"^[a-zA-Z]:\\(?:[^\\:*?""<>|\r\n]+\\)*[^\\:*?""<>|\r\n]*$";
Regex regex = new Regex(pattern);
return regex.IsMatch(path);
}
static string _appPath;
}
}