如何组织大量文件/目录路径常量
我有一个静态类,其中保留了大量在应用程序的不同位置使用的相对路径。看起来像这样:
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
中声明的所有目录和文件都在应用程序中的大量位置使用(每个路径可以多次使用,考虑到以下事实:有超过一百条路径 - 这是一个很大的数字)。我想保持此类的接口相同或对使用它们的其他类进行最小的更改。
I have a static class where I keep a large number of relative paths that are used in different places in my application. It looks like that:
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
}
All of them are relative to some parent directory, location of which I know only during the program run. I need to keep them all together because it allows me to easily control what files are used by my application and change their locations from time to time.
However even though they are organized in alphabetic order and its easy to find a certain path, I need to be able to change some of them depending on some setting. Lets say, there is a setting 'bool SettingA' and when I turn it on, I have to do modify some of the paths to use a different directory or a different file name.
The problem is that now I can't use constants, I have to rewrite my code to properties or methods so that I can change file paths at runtime. And here where my code becomes much bigger in size and the strict order now looks ugly. Is there a way I can group them, so that it will not confuse anybody who uses this code? I can't break them into a separate classes because it is difficult to remember in what class what constant you may keep. For now I'm grouping them by regions, but I have a bad feeling that keeping more than one hundred of properties in one class is wrong.
Edit:
All directories and files that I declare in FilePathConstants
are used in a large number of places in application (each path can be used multiple times, taking into account the fact that there is more then one hundred of paths - this is a large number). I would like to keep the interface of this class the same or with minimum changes to other classes that use them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许你可以使用行结构
maybe you could use rowstructs
使用“index”文件之类的东西来存储目录路径并在运行时加载它。
更新
我想建议使用间接 - “mapper”类。
它应该是这样的。
客户端示例:
索引文件格式:
<元素名称>|<路径><换行>
示例:
Use something like "index" file to store the directory paths and load it in runtime.
Update
I would like to suggest using indirection - "mapper" class.
Here is how it should look like.
Client example:
Index file format:
<Element name>|<Path><New Line>
Example:
你不能使用你的项目Properties.Settings吗?它存储在 .config 文件中,因此可以在部署后进行编辑
,或者只是不将它们设为常量,然后您可以在运行时编辑它们,但它们会在下次运行时恢复为原始设置。
或者不要使类静态并在每次使用时创建一个实例,然后更改所需的内容并在完成后丢弃该实例。
can you not use your projects Properties.Settings? it's stored in the .config file so can be edited after deployment
or just dont make them const, then you can edit them at runtime bu they revert to the original setting on next run.
or dont make the calss static and create an instance each time you use it, then change whats needed and discard the instance when finished.