GPPointFileInfo\InputFile.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\InputFile.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.IO;
using System.Security;
using System.Text.RegularExpressions;

namespace GPPointFileInfo
{
    public class InputFile : GPPointFileInfo.IInputFile
    {
        private int _index;
        private string _name;
        private string _path;

        public InputFile(IInputFileArray inputFileArray, int index)
        {
            _index = index;

            FileInfo fileInfo = GetFileInfo(inputFileArray, _index);

            string name = GetName(fileInfo);

            ValidateName(name);

            _name = name;

            string path = GetPath(fileInfo);

            ValidatePath(path);

            _path = path;
        }

        public int Index
        {
            get
            {
                return _index;
            }
        }

        private FileInfo GetFileInfo(IInputFileArray inputFileArray, int index)
        {
            return inputFileArray[index];
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        private string GetName(FileInfo fileInfo)
        {
            return fileInfo.Name;
        }

        private void ValidateName(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Name Is Null Or Empty.");
            }
            else if (IsBlank(name))
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Name Is Blank." + ObjectToString.GetNewline() + "name: " + ObjectToString.GetToString(name));
            }
        }

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

            bool isBlank;

            Regex regex = new Regex(Pattern);

            isBlank = regex.IsMatch(text);

            return isBlank;
        }

        public string Path
        {
            get
            {
                return _path;
            }
        }

        private string GetPath(FileInfo fileInfo)
        {
            return fileInfo.FullName;
        }

        private void ValidatePath(string path)
        {
            FileInfo fileInfo;

            try
            {
                fileInfo = new FileInfo(path);
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Unauthorized To Access Path." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path), ex);
            }
            catch (SecurityException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Insufficient Security Privileges To Access Path." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path), ex);
            }
            catch (ArgumentNullException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Is Null.", ex);
            }
            catch (PathTooLongException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Exceeds Maximum Allowable Length." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path), ex);
            }
            catch (NotSupportedException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Not Supported." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path), ex);
            }
            catch (ArgumentException ex)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Invalid Path." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path), ex);
            }

            if (!fileInfo.Exists)
            {
                throw new GPException(GPExceptionSeverity.Error, "Invalid Input File. Path Does Not Exist." + ObjectToString.GetNewline() + "path: " + ObjectToString.GetToString(path));
            }
        }
    }
}