using System;
using System.IO;
namespace Wayne.Lib.IO
{
///
/// Support class managing file paths of the Wayne system.
///
public class PathsImplementation : IPaths
{
#region DLL Import
[System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint = "GetModuleFileName", SetLastError = true)]
private static extern int GetModuleFileNameWinCE(IntPtr hModule, byte[] lpFilename, int nSize);
[System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "GetModuleFileNameW", SetLastError = true)]
private static extern int GetModuleFileNameWin32(IntPtr hModule, byte[] lpFilename, int nSize);
#endregion
#region Construction
///
/// Constructor.
///
///
public PathsImplementation(bool fullWindows)
{
//bool fullWindows = FileSupport.DirectoryExists(@"C:\");
if (fullWindows)
{
Root = @"C:\Wayne";
}
else
{
if (Directory.Exists(@"\Storage card"))
Root = @"\Storage card";
if (Directory.Exists(@"\Flash"))
Root = @"\Flash";
else
Root = @"\Wayne";
}
Config = Combine("Config");
Log = Combine("Log");
Transactions = Combine("Transactions");
Data = Combine("Data");
const int MAX_PATH = 260;
byte[] buffer = new byte[MAX_PATH * 2];
int chars;
if (fullWindows)
chars = GetModuleFileNameWin32(IntPtr.Zero, buffer, MAX_PATH);
else
chars = GetModuleFileNameWinCE(IntPtr.Zero, buffer, MAX_PATH);
if (chars > 0)
{
ExecutablePath = System.Text.Encoding.Unicode.GetString(buffer, 0, chars * 2);
ExecutableDirectory = Path.GetDirectoryName(ExecutablePath);
}
else
{
ExecutablePath = string.Empty;
ExecutableDirectory = string.Empty;
}
}
#endregion
#region Properties
///
/// The root path used by Wayne applications.
///
public string Root { get; private set; }
///
/// The base path to all config files.
///
public string Config { get; private set; }
///
/// The base path to all transaction files.
///
public string Transactions { get; private set; }
///
/// The base path to all log files.
///
public string Log { get; private set; }
///
/// The complete path (including the file name) to the executable.
///
public string ExecutablePath { get; private set; }
///
/// The directory path to the executable.
///
public string ExecutableDirectory { get; private set; }
///
/// The base path to data files.
///
public string Data { get; private set; }
#endregion
#region Methods
///
/// Combines the Root path and the given subPath.
///
/// The sub path (under the main path).
///
public string Combine(string subPath)
{
return Path.Combine(Root, subPath);
}
///
/// Combines the Root path and the given subPath and file name.
///
/// The sub path (under the main path).
/// An additional sub path (or the name of a file).
///
public string Combine(string subPath, string subPath2)
{
return Path.Combine(Path.Combine(Root, subPath), subPath2);
}
///
/// Returns the config path to the given config name.
///
/// The name of the config.
///
public string GetConfigPath(string configName)
{
return Path.Combine(Config, configName);
}
///
/// Returns the config path to the given config name.
///
/// The name of the config.
/// The name of a config file.
///
public string GetConfigPath(string configName, string fileName)
{
return Path.Combine(Path.Combine(Config, configName), fileName);
}
///
/// Returns the transaction file path to the given terminal type.
///
/// The name of the terminal type.
///
public string GetTransactionsPath(string terminalType)
{
return Path.Combine(Transactions, terminalType);
}
///
/// Returns the transaction file path to the given terminal type.
///
/// The name of the terminal type.
/// The name of the sub state.
///
public string GetTransactionsPath(string terminalType, string subState)
{
return Path.Combine(Path.Combine(Transactions, terminalType), subState);
}
///
/// Replaces the extension of a file (e.g. from C:\MyFile.aaa to C:\MyFile.bbb)
///
///
///
///
public string ReplaceExtension(string fileName, string newExtension)
{
string dir = Path.GetDirectoryName(fileName);
string name = Path.GetFileNameWithoutExtension(fileName);
return Path.Combine(dir, string.Concat(name, newExtension.StartsWith(".") ? string.Empty : ".", newExtension));
}
#endregion
#region Parse
///
/// Parses the path to replace any %...% variables with the current actual path.
///
///
///
public string Parse(string path)
{
if (path != null)
{
Replace(ref path, "%Root%", Root);
Replace(ref path, "%Config%", Config);
Replace(ref path, "%Transactions%", Transactions);
Replace(ref path, "%Log%", Log);
Replace(ref path, "%ExeDir%", ExecutableDirectory);
Replace(ref path, "%Data%", Data);
}
return path;
}
private static void Replace(ref string path, string variableName, string actualText)
{
int index = path.IndexOf(variableName, StringComparison.InvariantCultureIgnoreCase);
if (index > -1)
path = path.Remove(index, variableName.Length).Insert(index, actualText);
}
///
/// Checks if file exists
///
///
///
[Obsolete("Use the DirectoryExists() in FileSupport instead!")]
public bool FileExists(string fileName)
{
return FileSupport.FileExists(Parse(fileName));
}
///
/// Checks if directory exists
///
///
///
[Obsolete("Use the DirectoryExists() in FileSupport instead!")]
public bool DirectoryExists(string path)
{
return FileSupport.EnsureDirectoryExists(path);
}
///
/// Ensures the directory exists by trying to create it if it does not exist already.
///
///
///
[Obsolete("Use the DirectoryExists() in FileSupport instead!")]
public bool EnsureDirectoryExists(string path)
{
return FileSupport.EnsureDirectoryExists(path);
}
#endregion
}
}