Files
Common-VS2019/SacsStandardBase/Base/IO/DirectoryUtil.cs
T
2025-11-20 17:17:40 +00:00

156 lines
5.2 KiB
C#

using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
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 {ExceptionUtil.GetErrorStateMessage(exc)}. 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 {ExceptionUtil.GetErrorStateMessage(exc)}. 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 {ExceptionUtil.GetErrorStateMessage(exc)}. 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 {ExceptionUtil.GetErrorStateMessage(exc)}. Path: '{path}'. Pattern: {searchPattern}. Option: {searchOption}");
}
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 {ExceptionUtil.GetErrorStateMessage(exc)}. Path: '{path}'");
}
return null;
}
//-------------------------------------------------------------------
// Utilities.
public static String TrimTrailingSlashes(String path)
{
return Regex.Replace(path, "[\\\\/]+$", "");
}
//-------------------------------------------------------------------
}
//=======================================================================
}