GPPointFileInfo\InputFileArray.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\InputFileArray.cs
// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
// 

using System;
using System.Security;
using System.IO;

namespace GPPointFileInfo
{
    public class InputFileArray : GPPointFileInfo.IInputFileArray
    {
        private FileInfo[] _fileInfoArray;

        public InputFileArray(IGPInputFolder gpInputFolder, IGPInputFileSuffix gpInputFileSuffix, IGPRecurseInputSubfolders gpRecurseInputSubfolders)
        {
            FileInfo[] fileInfoArray = GetFileInfoArray(gpInputFolder, gpInputFileSuffix, gpRecurseInputSubfolders);

            ValidateFileInfoArray(fileInfoArray);

            _fileInfoArray = fileInfoArray;
        }

        public int Length
        {
            get
            {
                return _fileInfoArray.Length;
            }
        }

        public FileInfo this[int i]
        {
            get
            {
                return _fileInfoArray[i];
            }
        }

        private FileInfo[] GetFileInfoArray(IGPInputFolder gpInputFolder, IGPInputFileSuffix gpInputFileSuffix, IGPRecurseInputSubfolders gpRecurseInputSubfolders)
        {
            FileInfo[] fileInfoArray;

            string inputFileSearchPattern = GetSearchPattern(gpInputFileSuffix);

            SearchOption searchOption = GetSearchOption(gpRecurseInputSubfolders);

            DirectoryInfo directoryInfo;

            try
            {
                directoryInfo = new DirectoryInfo(gpInputFolder.Folder);
            }
            catch (PathTooLongException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Path To Input Folder Exceeds Maximum Allowable Length." + ObjectToString.GetNewline() + "gpInputFolder: " + ObjectToString.GetToString(gpInputFolder), ex);
            }
            catch (ArgumentNullException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Is Null.", ex);
            }
            catch (ArgumentException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Is Invalid." + ObjectToString.GetNewline() + "gpInputFolder: " + ObjectToString.GetToString(gpInputFolder), ex);
            }
            catch (SecurityException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Insufficient Security Privileges To Access Input Folder." + ObjectToString.GetNewline() + "gpInputFolder: " + ObjectToString.GetToString(gpInputFolder), ex);
            }

            try
            {
                fileInfoArray = directoryInfo.GetFiles(inputFileSearchPattern, searchOption);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Directory Not Found." + ObjectToString.GetNewline() + "gpInputFolder: " + ObjectToString.GetToString(gpInputFolder) + "inputFileSearchPattern: " + ObjectToString.GetToString(inputFileSearchPattern) + "searchOption: " + ObjectToString.GetToString(searchOption), ex);
            }

            return fileInfoArray;
        }

        private string GetSearchPattern(IGPInputFileSuffix gpInputFileSuffix)
        {
            const string WildcardPrefix = "*.";
            const string WildcardSuffix = "*";

            string searchPattern = WildcardPrefix;

            switch (gpInputFileSuffix.FileSuffix)
            {
                case null:
                    searchPattern += WildcardSuffix;
                    break;

                default:
                    searchPattern += gpInputFileSuffix.FileSuffix;
                    break;
            }

            return searchPattern;
        }

        private SearchOption GetSearchOption(IGPRecurseInputSubfolders recurseInputSubFolders)
        {
            return recurseInputSubFolders.RecurseSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
        }

        private void ValidateFileInfoArray(FileInfo[] fileInfoArray)
        {
            const int MinValidLength = 1;

            if (fileInfoArray.Length < MinValidLength)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. No Files Matching Search Criteria Found.");
            }
        }
    }
}