GPPointFileInfo\GPInputFolder.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPInputFolder.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
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.DataSourcesFile

Namespace GPPointFileInfo
  Public Class GPInputFolder
        Implements GPPointFileInfo.IGPInputFolder
    Private Shared _parameterIndex As Integer

    Private _folder As String

    Public Sub New(ByVal parameterValueArray As IParameterValueArray)
      Dim gpValue As IGPValue = parameterValueArray.GetGPValue(_parameterIndex)

      Dim folder As String = GetFolder(gpValue)

      ValidateFolder(folder)

      _folder = folder
    End Sub

    Public Shared Function GetGPParameterEdit() As IGPParameterEdit
      Dim gpParameterEdit As IGPParameterEdit = New GPParameterClass()

      Dim gpDataType As IGPDataType = New DEFolderTypeClass()

      Dim gpValue As IGPValue = New DEFolderClass()

      gpParameterEdit.Name = "in_input_folder"
      gpParameterEdit.DisplayName = "Input Folder"
      gpParameterEdit.DataType = gpDataType
      gpParameterEdit.Value = gpValue
      gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput
      gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeRequired

      Return gpParameterEdit
    End Function

    Public Shared Sub SetGPParameterIndex(ByVal index As Integer)
      _parameterIndex = index
    End Sub

        Public ReadOnly Property Folder() As String Implements IGPInputFolder.Folder
            Get
                Return _folder
            End Get
        End Property

    Private Function GetFolder(ByVal gpValue As IGPValue) As String
      Dim folder As String

      Dim deFolder As IDEFolder = TryCast(gpValue, IDEFolder)

      Dim dataElement As IDataElement = TryCast(deFolder, IDataElement)

      folder = dataElement.CatalogPath

      Return folder
    End Function

    Private Sub ValidateFolder(ByVal folder As String)
      Dim directoryInfo As DirectoryInfo

      Try
        directoryInfo = New DirectoryInfo(folder)
      Catch ex As PathTooLongException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Path To Input Folder Exceeds Maximum Allowable Length." & ObjectToString.GetNewline() & "folder: " & ObjectToString.GetToString(folder), 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() & "folder: " & ObjectToString.GetToString(folder), ex)
      Catch ex As SecurityException
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Insufficient Security Privileges To Access Input Folder." & ObjectToString.GetNewline() & "folder: " & ObjectToString.GetToString(folder), ex)
      End Try

      If (Not directoryInfo.Exists) Then
        Throw New GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Does Not Exist." & ObjectToString.GetNewline() & "folder: " & ObjectToString.GetToString(folder))
      End If
    End Sub
  End Class
End Namespace