我如何共享两个应用程序之间的配置设置

发布于 2025-01-17 21:05:56 字数 671 浏览 2 评论 0原文

我有两个用 C# 编写的应用程序。两者都执行相同的基本任务;一个具有 UI,旨在设置和配置流程,另一个是控制台应用程序,旨在按计划运行配置的流程。

我将它们在 Visual Studio 中设置为同一个解决方案中的两个不同项目,并使用一个单独的库,它们都引用完成所有艰苦的工作。在这两种情况下使用单个应用程序似乎不是推荐的方法,因此我相应地拆分了 UI/控制台变体。

问题是我正在尝试共享配置 - 只是一些字符串值;它们之间的连接字符串、文件位置等。这是从 UI 版本设置的,但控制台项目没有它就无法运行。

最初,我只是在 Application.exe.config 中进行了设置,但是当我使用常规安装程序安装该应用程序时,由于文件权限的原因,它在尝试保存配置文件时就死掉了。

现在,我在共享库中有一个 Settings 对象,其中包含这些值,因为用户设置可以通过其他项目都可以访问和更新的包装类进行访问。这解决了我的更新问题,但现在我的控制台应用程序无法读取 UI 设置的设置值。

我可以将它们放在一个配置文件中并手动处理它 - 这可能比尝试以 Windows 方式执行要快一些 - 但我觉得这一定是平台以某种方式支持的东西。

我应该如何存储我的配置,以便它可用于两个不同的应用程序(在同一 Visual Studio 解决方案中创建),并且两个应用程序使用的设置都可以由用户更新和保存?

I have two applications written in C#. Both do the same basic task; one has a UI and is designed to set-up and configure a process and the other is a console application designed to run the process as configured on a schedule.

I have them set up in Visual Studio as two different projects in the same Solution with a separate library they both reference doing all the hard work. Having a single application work in both circumstances doesn't seem to be a recommended approach, so I have split the UI/Console variants out accordingly.

The problem is that I am trying to share configuration - just a few string values; connection strings, file locations and so on - between them. This is set up from the UI version but the Console project can't run without it.

Initially I just had the settings in my Application.exe.config but when I installed the application using a regular installer, it died the moment it tried to save the configuration file because of file permissions.

Now I have a Settings object in the shared library with these values as User Settings made accessible through a wrapper class that both other projects can access and update. That solves my update problem, but now my Console application can't read the settings values that have been set by the UI.

I could just drop them in a configuration file and handle it manually - that would probably have been a little faster by this point than trying to do it the Windows way - but I feel like this must be something that the platform supports somehow.

How should I store my configuration so it is available to two different applications (created within the same Visual Studio Solution) and the settings used by both applications can be updated and saved by the user?

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

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

发布评论

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

评论(1

身边 2025-01-24 21:05:56

使用configurationManager类来管理您的app.config(请参阅docs 在这里)如果您想要内置的方式。您可能需要使用高度特权运行UI来编辑配置,也可以使配置文件特定于用户并将其存放在每个用户的配置文件中。

Use the ConfigurationManager class to manage your app.config (see the docs here) if you want the built-in way. You may need to run your UI with elevated privilege to edit the config or you can make the config files user specific and stash them under each user's profile.

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