GPPointFileInfo\InputFileArray.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\InputFileArray.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Security
Imports System.IO

Namespace GPPointFileInfo
  Public Class InputFileArray
        Implements GPPointFileInfo.IInputFileArray
    Private _fileInfoArray As FileInfo()

    Public Sub New(ByVal gpInputFolder As IGPInputFolder, ByVal gpInputFileSuffix As IGPInputFileSuffix, ByVal gpRecurseInputSubfolders As IGPRecurseInputSubfolders)
      Dim fileInfoArray As FileInfo() = GetFileInfoArray(gpInputFolder, gpInputFileSuffix, gpRecurseInputSubfolders)

      ValidateFileInfoArray(fileInfoArray)

      _fileInfoArray = fileInfoArray
    End Sub

        Public ReadOnly Property Length() As Integer Implements IInputFileArray.Length
            Get
                Return _fileInfoArray.Length
            End Get
        End Property

        Default Public ReadOnly Property Item(ByVal i As Integer) As FileInfo Implements IInputFileArray.Item
            Get
                Return _fileInfoArray(i)
            End Get
        End Property

    Private Function GetFileInfoArray(ByVal gpInputFolder As IGPInputFolder, ByVal gpInputFileSuffix As IGPInputFileSuffix, ByVal gpRecurseInputSubfolders As IGPRecurseInputSubfolders) As FileInfo()
      Dim fileInfoArray As FileInfo()

      Dim inputFileSearchPattern As String = GetSearchPattern(gpInputFileSuffix)

      Dim searchOption As SearchOption = GetSearchOption(gpRecurseInputSubfolders)

      Dim directoryInfo As DirectoryInfo

      Try
        directoryInfo = New DirectoryInfo(gpInputFolder.Folder)
      Catch ex As PathTooLongException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Path To Input Folder Exceeds Maximum Allowable Length." & ObjectToString.GetNewline() & "gpInputFolder: " & ObjectToString.GetToString(gpInputFolder), ex)
      Catch ex As ArgumentNullException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Is Null.", ex)
      Catch ex As ArgumentException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Is Invalid." & ObjectToString.GetNewline() & "gpInputFolder: " & ObjectToString.GetToString(gpInputFolder), ex)
      Catch ex As SecurityException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Insufficient Security Privileges To Access Input Folder." & ObjectToString.GetNewline() & "gpInputFolder: " & ObjectToString.GetToString(gpInputFolder), ex)
      End Try

      Try
        fileInfoArray = directoryInfo.GetFiles(inputFileSearchPattern, searchOption)
      Catch ex As DirectoryNotFoundException
        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)
      End Try

      Return fileInfoArray
    End Function

    Private Function GetSearchPattern(ByVal gpInputFileSuffix As IGPInputFileSuffix) As String
      Const WildcardPrefix As String = "*."
      Const WildcardSuffix As String = "*"

      Dim searchPattern As String = WildcardPrefix

      Select Case gpInputFileSuffix.FileSuffix
        Case Nothing
          searchPattern &= WildcardSuffix

        Case Else
          searchPattern &= gpInputFileSuffix.FileSuffix
      End Select

      Return searchPattern
    End Function

    Private Function GetSearchOption(ByVal recurseInputSubFolders As IGPRecurseInputSubfolders) As SearchOption
      If recurseInputSubFolders.RecurseSubfolders Then
        Return SearchOption.AllDirectories
      Else
        Return SearchOption.TopDirectoryOnly
      End If
    End Function

    Private Sub ValidateFileInfoArray(ByVal fileInfoArray As FileInfo())
      Const MinValidLength As Integer = 1

      If fileInfoArray.Length < MinValidLength Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. No Files Matching Search Criteria Found.")
      End If
    End Sub
  End Class
End Namespace