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

namespace GPPointFileInfo
{
    public class GPInputFileFormat : GPPointFileInfo.IGPInputFileFormat
    {
        private static int _parameterIndex;

        private string _fileFormat;

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

            string fileFormat = GetFileFormat(gpValue);

            ValidateFileFormat(fileFormat);

            _fileFormat = fileFormat;
        }

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

            IGPDataType gpDataType = new GPStringTypeClass();

            IGPCodedValueDomain gpCodedValueDomain = GetGPCodedValueDomain();

            IGPValue gpValue = gpCodedValueDomain.get_Value(0);

            IGPDomain gpDomain = gpCodedValueDomain as IGPDomain;

            gpParameterEdit.Name = "in_input_file_format";
            gpParameterEdit.DisplayName = "Input File Format";
            gpParameterEdit.DataType = gpDataType;
            gpParameterEdit.Value = gpValue;
            gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
            gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;
            gpParameterEdit.Domain = gpDomain;

            return gpParameterEdit;
        }

        private static IGPCodedValueDomain GetGPCodedValueDomain()
        {
            IGPCodedValueDomain gpCodedValueDomain = new GPCodedValueDomainClass();

            gpCodedValueDomain.AddStringCode("XYZ", "XYZ");
            gpCodedValueDomain.AddStringCode("GENERATE", "GENERATE");
            gpCodedValueDomain.AddStringCode("LAS", "LAS");

            return gpCodedValueDomain;
        }

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

        public string FileFormat
        {
            get
            {
                return _fileFormat;
            }
        }

        private string GetFileFormat(IGPValue gpValue)
        {
            string fileFormat;

            IGPString gpString = gpValue as IGPString;

            fileFormat = gpString.Value;

            return fileFormat;
        }

        private void ValidateFileFormat(string fileFormat)
        {
            IGPCodedValueDomain gpCodedValueDomain = GetGPCodedValueDomain();

            IGPString gpString = new GPStringClass();

            gpString.Value = fileFormat;

            IGPValue gpValue = gpString as IGPValue;

            string name;

            name = gpCodedValueDomain.FindName(gpValue);

            if (name == null)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File Format. Input File Format Does Not Fall Within Domain Of Valid Values." + ObjectToString.GetNewline() + "fileFormat: " + ObjectToString.GetToString(fileFormat));
            }
        }
    }
}