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

namespace GPPointFileInfo
{
    public class GPInputFileSuffix : GPPointFileInfo.IGPInputFileSuffix
    {
        private static int _parameterIndex;

        private string _fileSuffix;

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

            string fileSuffix = GetFileSuffix(gpValue);

            ValidateFileSuffix(fileSuffix);

            _fileSuffix = fileSuffix;
        }

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

            IGPDataType gpDataType = new GPStringTypeClass();

            IGPValue gpValue = new GPStringClass();

            gpParameterEdit.Name = "in_input_file_suffix";
            gpParameterEdit.DisplayName = "Input File Suffix";
            gpParameterEdit.DataType = gpDataType;
            gpParameterEdit.Value = gpValue;
            gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
            gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeOptional;

            return gpParameterEdit;
        }

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

        public string FileSuffix
        {
            get
            {
                return _fileSuffix;
            }
        }

        private string GetFileSuffix(IGPValue gpValue)
        {
            string fileSuffix;

            IGPString gpString = gpValue as IGPString;

            fileSuffix = gpString.Value;

            if (String.IsNullOrEmpty(fileSuffix) || IsBlank(fileSuffix))
            {
                fileSuffix = "*";
            }
            else
            {
                if (fileSuffix.StartsWith("."))
                {
                    fileSuffix = fileSuffix.Remove(0, 1);
                }
            }

            return fileSuffix;
        }

        private bool IsBlank(string text)
        {
            const string Pattern = "^\\s*$";

            bool isBlank;

            Regex regex = new Regex(Pattern);

            isBlank = regex.IsMatch(text);

            return isBlank;
        }

        private void ValidateFileSuffix(string fileSuffix)
        {
            char[] invalidFileSuffixCharacters = GetInvalidFileSuffixCharacters();

            int invalidFileSuffixCharacterIndex = fileSuffix.IndexOfAny(invalidFileSuffixCharacters);

            if (invalidFileSuffixCharacterIndex >= 0)
            {
                char invalidFileSuffixCharacter = fileSuffix[invalidFileSuffixCharacterIndex];

                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File Suffix. Input File Suffix Contains One Or More Invalid File Suffix Characters." + ObjectToString.GetNewline() + "fileSuffix: " + ObjectToString.GetToString(fileSuffix) + "invalidFileSuffixCharacter: " + ObjectToString.GetToString(invalidFileSuffixCharacter));
            }
        }

        private char[] GetInvalidFileSuffixCharacters()
        {
            const string Pattern = "[*?]";

            char[] invalidFileSuffixCharacters;

            Regex regex = new Regex(Pattern);

            string invalidFileNameString = new string(System.IO.Path.GetInvalidFileNameChars());

            invalidFileNameString = regex.Replace(invalidFileNameString, "");

            invalidFileSuffixCharacters = invalidFileNameString.ToCharArray();

            return invalidFileSuffixCharacters;
        }
    }
}