C# 如何组织大量文件/目录路径常量

C# 如何组织大量文件/目录路径常量,c#,file,path,code-organization,C#,File,Path,Code Organization,我有一个静态类,其中保存了大量在应用程序中不同位置使用的相对路径。看起来是这样的: static class FilePathConstants { public const string FirstDirectory = "First"; public const string FirstSecondDirectory = "First/Second"; public const string FirstSecondThirdFileA = "First/Second/

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

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
}
所有这些都是相对于某个父目录的,我只在程序运行期间知道它的位置。我需要将它们放在一起,因为它允许我轻松控制应用程序使用的文件,并随时更改它们的位置

然而,即使它们是按字母顺序组织的,并且很容易找到特定的路径,但我需要能够根据某些设置更改其中的一些路径。比如说,有一个设置'boolsettinga',当我打开它时,我必须修改一些路径以使用不同的目录或文件名

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

编辑:


我在
FilePathConstants
中声明的所有目录和文件都在应用程序中的大量位置使用(每个路径都可以多次使用,考虑到路径超过一百个,这是一个很大的数字)。我希望保持该类的接口不变,或者对使用它们的其他类进行最小的更改。

也许您可以使用rowstructs

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

const string indexFilePath = @"C:\dirlist.txt";
IEnumerable<string> paths = File.ReadAllLines(indexFilePath);
索引文件格式:
|

例如:

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

无法使用项目属性。设置?它存储在.config文件中,因此可以在部署后进行编辑

或者只是不将它们设置为常量,然后您可以在运行时编辑它们,但在下次运行时它们将恢复为原始设置


或者不要让Cals成为静态的,每次使用它时都创建一个实例,然后更改所需内容并在完成后丢弃实例。

什么叫“rowstructs”?它是一个半类,一个类,但在这种情况下要轻得多,而且很有用,因为对于这种用法,类可能会“沉重”,建议使用rowstructsLets,比如我使用索引文件。然后,我必须为每一行创建一个方法或属性。看起来我又回到了以前的状态,
paths
变量包含了所有的目录,所以你可以通过目录进行枚举。这意味着每次我需要得到一个路径,我必须枚举一百个路径,我不能保证它包含我的路径。请你提供一些更多的上下文信息:你的程序中如何使用目录路径?我不使用Properties.Settings。问题是,我需要将它们从常量更改为属性,但这会使我的代码变得非常庞大和丑陋,我需要以某种方式组织这些路径
        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);
FirstDirectory|First
FirstSecondDirectory|First\Second
FirstSecondThirdFileA|First\Second\Third\FileA
FirstSecondFourthFileB|First\Second\Fourth\FileB