// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// Defines the DirectoryUtilities type.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Utilities
{
using System;
using System.IO;
using System.Windows;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
///
/// The directory utilities.
///
public class DirectoryUtilities
{
///
/// The get user storage path.
///
///
/// The is nightly.
///
///
/// The .
///
public static string GetUserStoragePath(bool isNightly)
{
if (isNightly)
{
return Path.Combine(GetStorageDirectory(), "HandBrake", "Nightly");
}
else
{
return Path.Combine(GetStorageDirectory(), "HandBrake");
}
}
///
/// Get the app default log directory.
///
///
/// The .
///
public static string GetLogDirectory()
{
return Path.Combine(GetStorageDirectory(), "HandBrake", "logs");
}
///
/// Simple way of checking if a directory is writeable.
///
/// Path to check
///
/// Prompt to create directory if it doesn't exist.
///
///
/// An instance of the error service to allow prompting.
///
/// True if writable
public static bool IsWritable(string dirPath, bool createDirectoryPrompt, IErrorService errorService)
{
try
{
if (!Directory.Exists(dirPath))
{
MessageBoxResult result = errorService.ShowMessageBox(string.Format(Resources.DirectoryUtils_CreateFolderMsg, dirPath), Resources.DirectoryUtils_CreateFolder, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Directory.CreateDirectory(dirPath);
}
}
using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose))
{
}
return true;
}
catch
{
return false;
}
}
///
/// The get storage directory.
///
///
/// The storage directory. Either AppData or portable location.
///
private static string GetStorageDirectory()
{
string storagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (Portable.IsPortable())
{
storagePath = Portable.GetStorageDirectory();
}
return storagePath;
}
}
}