|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Directory.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Directory class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using Catel.Properties;
namespace Catel.IO
{
/// <summary>
/// Directory class.
/// </summary>
public class Directory
{
/// <summary>
/// Creates the specified directory.
/// </summary>
/// <param name="path">The path to create.</param>
public static void CreateDirectory(string path)
{
System.IO.Directory.CreateDirectory(path);
}
/// <summary>
/// Checks whether the specified directory exists.
/// </summary>
/// <param name="directory">The directory to check.</param>
/// <returns><c>true</c> if the directory exists; otherwise <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">when <paramref name="directory"/> is <c>null</c>.</exception>
public static bool Exists(string directory)
{
if (directory == null)
{
throw new ArgumentNullException("directory");
}
if (string.IsNullOrEmpty(directory))
{
return false;
}
directory = Path.AppendTrailingSlash(directory) + "*";
return File.Exists(directory);
}
/// <summary>
/// Deletes the specified directory.
/// </summary>
/// <param name="directory">The directory.</param>
/// <returns><c>true</c> if the directory is deleted; otherwise <c>false</c>.</returns>
/// <exception cref="ArgumentException">when <paramref name="directory"/> is <c>null</c> or empty.</exception>
public static bool Delete(string directory)
{
if (string.IsNullOrEmpty(directory))
{
throw new ArgumentException(Exceptions.ArgumentCannotBeNullOrEmpty, "directory");
}
IntPtr invalidHandleValue = new IntPtr(-1);
Win32IOApi.WIN32_FIND_DATA findFileData;
directory = Path.AddWin32LongFileNamePrefix(directory);
string originalDirectory = directory;
directory = Path.AppendTrailingSlash(directory) + "*";
IntPtr findHandle = Win32IOApi.FindFirstFile(directory, out findFileData);
if (findHandle == invalidHandleValue)
{
return false;
}
bool search = true;
while (search)
{
if (Win32IOApi.FindNextFile(findHandle, out findFileData))
{
// Is this the current or top directory, then continue
if ((findFileData.cFileName == ".") || (findFileData.cFileName == ".."))
{
continue;
}
string fileName = Path.Combine(originalDirectory, findFileData.cFileName);
// Is this a directory?
if ((findFileData.dwFileAttributes & Win32IOApi.FILE_ATTRIBUTE_DIRECTORY) == Win32IOApi.FILE_ATTRIBUTE_DIRECTORY)
{
if (!Delete(fileName))
{
Win32IOApi.FindClose(findHandle);
return false;
}
}
else
{
// Is this a read-only file?
if ((findFileData.dwFileAttributes & Win32IOApi.FILE_ATTRIBUTE_READONLY) == Win32IOApi.FILE_ATTRIBUTE_READONLY)
{
// Remove read-only flag
Win32IOApi.SetFileAttributes(fileName, findFileData.dwFileAttributes - Win32IOApi.FILE_ATTRIBUTE_READONLY);
}
if (!File.Delete(fileName))
{
Win32IOApi.FindClose(findHandle);
return false;
}
}
}
else
{
// No more files there
if (Marshal.GetLastWin32Error() == Win32IOApi.ERROR_NO_MORE_FILES)
{
search = false;
}
else
{
Win32IOApi.FindClose(findHandle);
return false;
}
}
}
Win32IOApi.FindClose(findHandle);
return Win32IOApi.RemoveDirectory(originalDirectory);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.