C# 应用程序和配置设置

发布于 2024-09-05 22:33:18 字数 674 浏览 2 评论 0原文

我以前从未使用过 Settings 类,但我在 CodeProject 上找到了一些我目前正在阅读的文章(例如 http://www.codeproject.com/KB/cs/PropertiesSettings.aspx),但到目前为止我还没有看到如何保存 string下次启动应用程序后获取它的数组。

例如,我的应用程序有多个 FileSystemWatcher 实例,每个实例都连接几个其他目录(例如,一个 FSW 实例正在监视一个目录的更改,当发生更改时,它将某些文件复制到其他几个目录) ,所以我将有一个 string 数组,其中包含表示 FSW 实例的监视路径,以及每个路径的 string 数组,表示受影响的目录。

我的问题是,我应该使用什么(Settings 类或其他类),以及如何使用它来存储可变数量的 string 数组的应用程序配置?强调的是我可以很快使用的东西,因为我没有太多时间来制作自定义类(但如果我找不到解决方案,则必须这样做)或深入研究一些晦涩的黑客。 任何教程链接、代码片段都会非常有帮助。

谢谢。

I never used Settings class before but I found some articles on CodeProject which I'm currently reading (for example http://www.codeproject.com/KB/cs/PropertiesSettings.aspx) but so far I didn't see how to save string array for getting it after application is started next time.

For example my application has several FileSystemWatcher instances, with each instance several other directories are connected (for example one FSW instance is monitoring one directory for a change and when it happens it copies some file to several other directories), so I would have one string array with watched paths representing FSW instances, and string array for each of those paths, representing directories that are affected.

My question is, what should I use (Settings class or something else), and how should I use that for storing application configuration that is variable number of string arrays? Emphasize is on something I could use very soon as I don't have too much time to make custom class (but would have to if I cannot find solution) or dig into some obscure hacks.
Any tutorial link, code snippet would be very helpful.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-09-12 22:33:18

为什么不使用配置文件呢?我有一组像你一样的 FileSystemWatchers,只是使用 自定义配置添加了一组路径部分。认为这需要您滚动一个类来扩展配置类,但我认为您无法击败这种方法。

不过,如果您想要一个清晰简单的破解并且不想被自定义配置部分/元素/集合所困扰。只需使用快速简单的 AppSettings 技巧即可。 XD

<appSettings>
    <add key="ConnectionInfo0" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo1" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo2" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>

从代码中获取它。

const string CONNECTIONSTRING="";
int i = 0;
while(ConfigurationSettings.AppSettings["ConnectionInfo"] + i != null)
{
    // do stuff with connection info here
    // use
    ConfigurationSettings.AppSettings["ConnectionInfo" + i];
    // to get data.
    ++i;
}

有点丑,我知道。自定义配置效果最好。

Why not use a config file instead? I had a set of FileSystemWatchers like yours and just added a set of paths using custom config sections. Thought that requires you rolling a class to extending the Configuration classes, but I think that you can't beat that approach.

Though if you want a clear easy hack and don't want to get bothered with the Custom Config Sections/Elements/Collections. Just use a quick and easy AppSettings hack. XD

<appSettings>
    <add key="ConnectionInfo0" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo1" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo2" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>

Getting it from code.

const string CONNECTIONSTRING="";
int i = 0;
while(ConfigurationSettings.AppSettings["ConnectionInfo"] + i != null)
{
    // do stuff with connection info here
    // use
    ConfigurationSettings.AppSettings["ConnectionInfo" + i];
    // to get data.
    ++i;
}

Tad ugly, I know. Custom Config works best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文