Share via

Validate folder exists and Validate folder is not empty ?

G S 0 Reputation points
2026-02-13T07:07:03.74+00:00

Hi,

In the following method I didn't handle case of folder with extension of .gdb.

The needed checks:

I need the factory check.

Validate file exists is working => for folder

Validate file is not empty => for folder

And if all pass return

factory.TryCreate("Gdb", out converter)

How is can be done in best practice ?

I'm using .Net 10 on Windows ?

Thanks in advance,

/// <summary>

 /// Attempts to create a converter for the specified GIS input file path with detailed diagnostic information.

 /// </summary>

 /// <param name="factory">The converter factory to use.</param>

 /// <param name="gisInputFilePath">The path to the GIS input file.</param>

 /// <param name="converter">When successful, contains the created converter; otherwise, <c>null</c>.</param>

 /// <param name="detectedSourceFormat">Contains diagnostic information about the detection source format process  (whether "GeoJson", "Shapefile", etc.).</param>

 /// <param name="detectReason">Contains diagnostic information about the detection process.</param>

 /// <returns><c>true</c> if converter creation succeeded; otherwise, <c>false</c>.</returns>

 /// <exception cref="ArgumentNullException">Thrown when <paramref name="factory"/> is null.</exception>

 /// <remarks>

 /// Detection priority:

 /// 1. Archives: Fast-path extension checks → KMZ detection → JSON voting → requirement matching

 /// 2. Single files: Explicit extension mapping → JSON format detection (with fallback) → generic extension mapping

 /// 

 /// All file reads are bounded and streaming. Archives are never extracted to disk.

 /// </remarks>

 public static bool TryCreateForInput(

     this IConverterFactory factory,

     string gisInputFilePath,

     out IConverter? converter,

     out string? detectedSourceFormat,

     out string? detectReason)

 {

     converter = null;

     detectReason = null;

     detectedSourceFormat = null;

     if (factory == null) throw new ArgumentNullException(nameof(factory));

     // Validate input path is not null or empty

     if (string.IsNullOrWhiteSpace(gisInputFilePath))

     {

         detectReason = "input path is null or whitespace";

         Log.Error($"TryCreateForInput: {detectReason}");

         return false;

     }

     // Validate file exists

     if (!File.Exists(gisInputFilePath))

     {

         detectReason = $"file does not exist: {gisInputFilePath}";

         Log.Error($"TryCreateForInput: {detectReason}");

         return false;

     }

     // Validate file is not empty

     FileInfo fileInfo;

     try

     {

         fileInfo = new FileInfo(gisInputFilePath);

     }

     catch (Exception ex)

     {

         detectReason = $"failed to get file info: {ex.Message}";

         Log.Error($"TryCreateForInput: {detectReason}", ex);

         return false;

     }

     if (fileInfo.Length == 0)

     {

         detectReason = "file is empty (0 bytes)";

         Log.Warn($"TryCreateForInput: {detectReason}");

         return false;

     }

     try

     {

         return ConverterUtils.IsArchiveFile(gisInputFilePath)

             ? TryDetectArchiveFormat(factory, gisInputFilePath, out converter, out detectedSourceFormat, out detectReason)

             : TryDetectSingleFileFormat(factory, gisInputFilePath, out converter, out detectedSourceFormat, out detectReason);

     }

     catch (Exception ex)

     {

         detectReason = $"unexpected error during format detection: {ex.GetType().Name} - {ex.Message}";

         Log.Error(detectReason, ex);

         return false;

     }

 }

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

{count} votes

1 answer

Sort by: Most helpful
  1. Varsha Dundigalla(INFOSYS LIMITED) 4,780 Reputation points Microsoft External Staff
    2026-02-16T11:32:58.08+00:00

    Thank you for reaching out.

    This behavior is expected because a .gdb input is not a single file. A .gdb (File Geodatabase) is actually a folder on disk that contains many internal files. Because of this, file‑based checks like File.Exists or file size work for formats such as GeoJSON, but they do not work for .gdb inputs.

    For a .gdb input, validation should be treated as folder validation, not file validation. In simple terms:

    • The path should be checked as a folder, not a file.
    • The folder should exist.
    • The folder should not be empty (it should contain data files).
    • Once these checks pass, the converter factory can safely proceed with creating the Gdb converter.

    This is a path‑validation concern in .NET, not an issue with the converter logic itself. The key point is handling folder‑based GIS formats correctly before calling the factory.

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.