【问题标题】:How to organize a large number of file/directory path constants如何组织大量文件/目录路径常量
【发布时间】:2012-02-25 09:50:23
【问题描述】:

我有一个静态类,我在其中保留了大量在我的应用程序的不同位置使用的相对路径。看起来是这样的:

static class FilePathConstants
{
    public const string FirstDirectory = "First";
    public const string FirstSecondDirectory = "First/Second";
    public const string FirstSecondThirdFileA = "First/Second/Third/FileA";
    public const string FirstSecondFourthFileB = "First/Second/Fourth/FileB";
    ... nearly 100 of similar members
}

所有这些都相对于某个父目录,我仅在程序运行期间才知道其位置。我需要将它们放在一起,因为它可以让我轻松控制我的应用程序使用哪些文件并不时更改它们的位置。

但是,尽管它们是按字母顺序组织的并且很容易找到某个路径,但我需要能够根据某些设置更改其中的一些。可以说,有一个设置“bool SettingA”,当我打开它时,我必须修改一些路径以使用不同的目录或不同的文件名。

问题是现在我不能使用常量,我必须将我的代码重写为属性或方法,以便我可以在运行时更改文件路径。在这里,我的代码变得更大,严格的顺序现在看起来很难看。有没有办法可以对它们进行分组,以免混淆使用此代码的任何人?我不能将它们分成单独的类,因为很难记住在哪个类中你可以保持什么常量。现在我按地区对它们进行分组,但我有一种不好的感觉,在一个类中保留超过一百个属性是错误的。

编辑:

我在FilePathConstants中声明的所有目录和文件在应用程序中大量使用(每个路径可以多次使用,考虑到有超过一百个路径的事实 - 这是大量)。我想保持这个类的接口相同或对使用它们的其他类的更改最少。

【问题讨论】:

    标签: c# file path code-organization


    【解决方案1】:

    也许你可以使用 rowstructs

    【讨论】:

    • “rowstructs”是什么意思?
    • 它是一个半类,一个类,但在这种情况下要轻得多且有用,其中一个类可能会“重”用于这种用法,建议使用 rowstructs
    【解决方案2】:

    使用类似“索引”的文件来存储目录路径并在运行时加载它。

    const string indexFilePath = @"C:\dirlist.txt";
    IEnumerable<string> paths = File.ReadAllLines(indexFilePath);
    

    更新

    我想建议使用间接 - “映射器”类。 这是它的外观。

    public enum FileSystemElement
    {
        FirstDirectory,
        FirstSecondDirectory,
        FirstSecondThirdFileA,
        FirstSecondFourthFileB
    }
    
    public class FileSystemMapper
    {
        private readonly string _rootDirectory;
        private readonly Dictionary<FileSystemElement, string> _fileElements;
    
        public FileSystemMapper(string rootDirectory, string fileName)
        {
            _rootDirectory = rootDirectory;
            string[] lines = File.ReadAllLines(fileName);
            _fileElements = lines.Select(ParsePair).ToDictionary(pair => pair.Key, pair => pair.Value);
        }
    
        public string GetPath(FileSystemElement element)
        {
            string relativePath;
            if (!_fileElements.TryGetValue(element, out relativePath))
            {
                throw new InvalidOperationException("Element not found");
            }
    
            string resultPath = Path.Combine(_rootDirectory, relativePath);
            return resultPath;
        }
    
        private static KeyValuePair<FileSystemElement, string> ParsePair(string line)
        {
            const string separator = "|";
    
            // File element alias | Path
            if (string.IsNullOrEmpty(line))
                throw new ArgumentException("Null or empty line", "line");                
    
            string[] components = line.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
            if (components.Length != 2)
                throw new ArgumentException("Line has invalid format", "line");
    
            FileSystemElement element;
            bool parseResult = FileSystemElement.TryParse(components[0], out element);
            if (!parseResult)
                throw new ArgumentException("Invalid element name", "line");
    
            string path = components[1]; // for clarity
            return new KeyValuePair<FileSystemElement, string>(element, path);
        }
    

    客户端示例:

            FileSystemMapper fileSystemMapper = new FileSystemMapper(@"C:\root", @"C:\dirs.txt");
            string firstDirectory = fileSystemMapper.GetPath(FileSystemElement.FirstDirectory);
            string secondDirectory = fileSystemMapper.GetPath(FileSystemElement.FirstSecondDirectory);
            string secondThirdFile = fileSystemMapper.GetPath(FileSystemElement.FirstSecondThirdFileA);
    

    索引文件格式:&lt;Element name&gt;|&lt;Path&gt;&lt;New Line&gt;

    例子:

    FirstDirectory|First
    FirstSecondDirectory|First\Second
    FirstSecondThirdFileA|First\Second\Third\FileA
    FirstSecondFourthFileB|First\Second\Fourth\FileB
    

    【讨论】:

    • 假设我使用索引文件。然后对于每一行,我都必须创建一个方法或属性。看来我要回到原来的地方了
    • 不,paths 变量包含你所有的目录,所以你可以通过目录枚举。
    • 意思是每次需要获取路径,都要枚举一百条路径,根本无法保证它包含我的路径
    • 能否请您提供更多上下文信息:您的程序中使用的目录路径如何?
    【解决方案3】:

    你不能使用你的项目 Properties.Settings 吗?它存储在 .config 文件中,因此可以在部署后进行编辑

    或者只是不将它们设为 const,然后您可以在运行时对其进行编辑,但它们会在下次运行时恢复为原始设置。

    或者不要将 calss 设为静态并在每次使用时创建一个实例,然后更改所需内容并在完成后丢弃该实例。

    【讨论】:

    • 我不使用 Properties.Settings。问题是我需要将它们从常量更改为属性,但这会使我的代码变得非常庞大和丑陋,我需要以某种方式组织这些路径
    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2023-04-07
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多