Files
Common-VS2019/SacsStandardBase/Base/IO/DirectoryUtil.cs
T
2021-05-05 15:36:28 +01:00

147 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
namespace Sarand.Net.Standard.Base.IO
{
//=======================================================================
public static class DirectoryUtil
{
//-------------------------------------------------------------------
// D i r e c t o r y E x i s t s
//-------------------------------------------------------------------
public static bool DirectoryExists (String directory)
{
try
{
return Directory.Exists(directory);
}
catch (Exception) { }
return false;
}
//-------------------------------------------------------------------
// G e t D i r e c t o r y N a m e
//-------------------------------------------------------------------
/// <summary>
/// Returns the directory information for the specified path string.
/// </summary>
/// <param name="path">The path of a file or directory.</param>
/// <returns>Directory information for path, or null if path denotes a root directory or is
/// null. Returns System.String.Empty if path does not contain directory information.</returns>
public static String GetDirectoryName (String path)
{
try
{
// Exceptions:
// T:System.ArgumentException:
// The path parameter contains invalid characters, is empty, or contains only white spaces.
// T:System.IO.PathTooLongException:
// The path parameter is longer than the system-defined maximum length.
return Path.GetDirectoryName(path);
}
catch (Exception) { }
return String.Empty;
}
//-------------------------------------------------------------------
// E n u m e r a t e D i r e c t o r i e s
//-------------------------------------------------------------------
/// <summary>
/// Retrieve list of sub directories in directory.
/// <para>Avoiding Exception thrown when network path isn't valid.</para>
/// </summary>
public static IEnumerable<String> EnumerateDirectories (String path)
{
try
{
return Directory.EnumerateDirectories(path);
}
catch (Exception exc)
{
SaLogger.Error("DirectoryUtil.EnumerateDirectories " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'");
}
return new List<String>();
}
//-------------------------------------------------------------------
// E n u m e r a t e F i l e s
//-------------------------------------------------------------------
/// <summary>
/// Retrieve list of files in directory.
/// <para>Avoiding Exception thrown when network path isn't valid.</para>
/// </summary>
public static IEnumerable<String> EnumerateFiles (String path)
{
try
{
return Directory.EnumerateFiles(path);
}
catch (Exception exc)
{
SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'");
}
return new List<String>();
}
//-------------------------------------------------------------------
/// <summary>
/// Retrieve list of files in directory.
/// <para>Avoiding Exception thrown when network path isn't valid.</para>
/// </summary>
public static IEnumerable<String> EnumerateFiles (String path, String searchPattern)
{
try
{
return Directory.EnumerateFiles(path, searchPattern);
}
catch (Exception exc)
{
SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'. Pattern: " + searchPattern);
}
return new List<String>();
}
//-------------------------------------------------------------------
/// <summary>
/// Retrieve list of files in directory.
/// <para>Avoiding Exception thrown when network path isn't valid.</para>
/// </summary>
public static IEnumerable<String> EnumerateFiles (String path, String searchPattern, SearchOption searchOption)
{
try
{
return Directory.EnumerateFiles(path, searchPattern, searchOption);
}
catch (Exception exc)
{
SaLogger.Error("DirectoryUtil.EnumerateFiles " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'. Pattern: " + searchPattern + ". Option: " + searchOption.ToString());
}
return new List<String>();
}
//-------------------------------------------------------------------
// C r e a t e D i r e c t o r y
//-------------------------------------------------------------------
/// <summary>
/// Create Directory in the specified path.
/// <para>Avoiding Exception thrown when network path isn't valid.</para>
/// </summary>
public static DirectoryInfo CreateDirectory (String path)
{
try
{
return Directory.CreateDirectory(path);
}
catch (Exception exc)
{
SaLogger.Error("DirectoryUtil.CreateDirectory " + exc.GetType().Name + ": " + exc.Message + ". Path: '" + path + "'");
}
return null;
}
//-------------------------------------------------------------------
}
//=======================================================================
}