private void Initialize(string rootDirectory, List<string> ensuredDirectories)
{
if(ensuredDirectories == null) ensuredDirectories = new List<string>();
if(String.IsNullOrEmpty(rootDirectory) || rootDirectory.IndexOfAny(Path.GetInvalidPathChars()) != -1) throw new ArgumentException("rootDirectory must be a valid file path.");
if(!Path.IsPathRooted(rootDirectory)) rootDirectory = Path.Combine(Environment.CurrentDirectory, rootDirectory);
_rootDirectory = rootDirectory;
if(!Directory.Exists(_rootDirectory)) Directory.CreateDirectory(_rootDirectory);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
List<string> sourceDirectories = ensuredDirectories;
bool logsEnsured = sourceDirectories.Contains("Logs", comparer);
bool dataEnsured = sourceDirectories.Contains("Data", comparer);
bool cacheEnsured = sourceDirectories.Contains("Cache", comparer);
if(sourceDirectories.Count == 0)
{
if(!logsEnsured) sourceDirectories.Add("Logs");
if(!dataEnsured) sourceDirectories.Add("Data");
if(!cacheEnsured) sourceDirectories.Add("Cache");
}
else
{
if(!cacheEnsured) sourceDirectories.Insert(0, "Cache");
if(!dataEnsured) sourceDirectories.Insert(0, "Data");
if(!logsEnsured) sourceDirectories.Insert(0, "Logs");
}
List<string> actualEnsuredDirectories = new List<string>();
foreach(string t in sourceDirectories)
{
string currentDir = t;
if(String.IsNullOrEmpty(currentDir)) continue;
try
{
currentDir = Path.IsPathRooted(currentDir) ? currentDir : Path.Combine(_rootDirectory, currentDir);
}
catch(Exception ex)
{
Dev2Logger.Log.Error(ex);
currentDir = null;
}
if(currentDir != null)
{
try
{
if(!Directory.Exists(currentDir)) Directory.CreateDirectory(currentDir);
}
catch(Exception ex)
{
Dev2Logger.Log.Error(ex);
currentDir = null;
}
if(currentDir != null) actualEnsuredDirectories.Add(currentDir);
}
}
_ensuredDirectories = actualEnsuredDirectories.ToArray();
}
#endregion