using System.IO; namespace Deltares.Maps { public interface IFile { bool IsValid { get; } string NameWithoutExtension { get; } string FileName { get; } string DirectoryName { get; } string FullPath { get; } } public abstract class FileLocation : IFile { const string TheGeometryFileIsNotValid = "The file is not valid"; private readonly string fullPath; /// /// Create a file location with the current directory /// /// /// protected FileLocation(string fileName, string fileExtension) : this(string.Empty, fileName, fileExtension) { } protected FileLocation(string fileLocation, string fileName, string fileExtension) { if (IsNullOrEmptyOrWhiteSpace(fileName)) { return; } fullPath = fileName; if (IsNullOrEmptyOrWhiteSpace(fileExtension)) { return; } if (!fileName.EndsWith(fileExtension)) { fileName += fileExtension; } if (IsNullOrEmptyOrWhiteSpace(fileLocation) || fileLocation == "." || fileLocation == @".\") { fullPath = Path.Combine(Directory.GetCurrentDirectory(), fileName); } else { fullPath = Path.Combine(fileLocation, fileName); } } public bool IsValid { get { return HasValidData(FileName, FileExtension); } } private static bool HasValidData(string fileName, string fileExtension) { if (IsNullOrEmptyOrWhiteSpace(fileExtension)) { return false; } return !(IsNullOrEmptyOrWhiteSpace(fileName) || !fileName.EndsWith(fileExtension)); } private static bool IsNullOrEmptyOrWhiteSpace(string value) { return string.IsNullOrEmpty(value) || value.Trim() == string.Empty; } public bool Exists { get { return File.Exists(FullPath); } } /// /// Gets the name of the directory the file is contained in /// public string DirectoryName { get { return Path.GetDirectoryName(fullPath); } } /// /// Gets the file extension /// public string FileExtension { get { return Path.GetExtension(fullPath); } } /// /// Gets the name of the file without the path. Includes the extension /// public string FileName { get { return Path.GetFileName(fullPath); } } /// /// Gets the full path of the file (including the file name) /// // ReSharper disable ConvertToAutoPropertyWithPrivateSetter public string FullPath // ReSharper restore ConvertToAutoPropertyWithPrivateSetter { get { return fullPath; } } /// /// Gets the name of the file without extension and location /// public string NameWithoutExtension { get { return Path.GetFileNameWithoutExtension(fullPath); } } public override string ToString() { return FullPath; } } public class ShapeFileLocation : FileLocation { public const string FileExtensionString = ".shp"; public ShapeFileLocation(string fileName) : base(fileName, FileExtensionString) { } /// /// /// The location or folder of the file /// The name of the file public ShapeFileLocation(string fileLocation, string fileName) : base(fileLocation, fileName, FileExtensionString) { } } public class WktFileLocation : FileLocation { public const string FileExtensionString = ".wkt"; public WktFileLocation(string fileName) : base(fileName, FileExtensionString) { } /// /// /// /// public WktFileLocation(string fileLocation, string fileName) : base(fileLocation, fileName, FileExtensionString) { } } }