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

namespace GPPointFileInfo
{
    public class GPInputFolder : GPPointFileInfo.IGPInputFolder
    {
        private static int _parameterIndex;

        private string _folder;

        public GPInputFolder(IParameterValueArray parameterValueArray)
        {
            IGPValue gpValue = parameterValueArray.GetGPValue(_parameterIndex);

            string folder = GetFolder(gpValue);

            ValidateFolder(folder);

            _folder = folder;
        }

        public static IGPParameterEdit GetGPParameterEdit()
        {
            IGPParameterEdit gpParameterEdit = new GPParameterClass(); ;

            IGPDataType gpDataType = new DEFolderTypeClass();

            IGPValue gpValue = 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;
        }

        public static void SetGPParameterIndex(int index)
        {
            _parameterIndex = index;
        }

        public string Folder
        {
            get
            {
                return _folder;
            }
        }

        private string GetFolder(IGPValue gpValue)
        {
            string folder;

            IDEFolder deFolder = gpValue as IDEFolder;

            IDataElement dataElement = deFolder as IDataElement;

            folder = dataElement.CatalogPath;

            return folder;
        }

        private void ValidateFolder(string folder)
        {
            DirectoryInfo directoryInfo;

            try
            {
                directoryInfo = new DirectoryInfo(folder);
            }
            catch (PathTooLongException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Path To Input Folder Exceeds Maximum Allowable Length." + ObjectToString.GetNewline() + "folder: " + ObjectToString.GetToString(folder), 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() + "folder: " + ObjectToString.GetToString(folder), ex);
            }
            catch (SecurityException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Insufficient Security Privileges To Access Input Folder." + ObjectToString.GetNewline() + "folder: " + ObjectToString.GetToString(folder), ex);
            }

            if (!directoryInfo.Exists)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input Folder. Input Folder Does Not Exist." + ObjectToString.GetNewline() + "folder: " + ObjectToString.GetToString(folder));
            }
        }
    }
}