找不到我的配置文件

发布于 2024-12-28 15:10:58 字数 1064 浏览 0 评论 0原文

我需要读取/写入与任何 exe 无关的配置文件。我正在尝试这样做:

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);
        if(appConfiguration == null) {

           //Configuration file not found, so throw an exception
           //TODO: thow an exception here
        } else {

           //Have Configuration, so work on the contents
           var fileEnvironment = appConfiguration.GetSection("fileEnvironment");
        }

没有抛出异常,但 fileEnvironment 始终为空。文件内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="fileEnvironment" type="System.Configuration.NameValueSectionHandler"/>
   </configSections>

   <fileEnvironment>
      <add key="DxStudioLocation" value="123456"/>
   </fileEnvironment>
</configuration>

请有人带我走出荒野。在获得该部分的内容后,我也不知道如何编写或更改 NameValueCollection 中的条目。 谢谢

I need to read/write to a config file not related to any exe. I'm trying this:

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);
        if(appConfiguration == null) {

           //Configuration file not found, so throw an exception
           //TODO: thow an exception here
        } else {

           //Have Configuration, so work on the contents
           var fileEnvironment = appConfiguration.GetSection("fileEnvironment");
        }

No exception is thrown, but fileEnvironment is always null. Here is the file contents:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="fileEnvironment" type="System.Configuration.NameValueSectionHandler"/>
   </configSections>

   <fileEnvironment>
      <add key="DxStudioLocation" value="123456"/>
   </fileEnvironment>
</configuration>

Someone please lead me out of the wilderness. I also don't know how to write, or change, an entry in the NameValueCollection, after I get the contents of the section.
Thanks

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

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

发布评论

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

评论(2

最丧也最甜 2025-01-04 15:10:58

您可以通过一些细微调整来全球化 AppSettingsSection

<section name="fileEnvironment" type="System.Configuration.AppSettingsSection"/>

: :

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);

        if (!appConfiguration.HasFile) // no need to null check, ConfigurationManager.OpenMappedExeConfiguration will always return an object or throw ArgumentException
        {
            //Configuration file not found, so throw an exception
        }
        else
        {
            var section = appConfiguration.GetSection("fileEnvironment") as AppSettingsSection;
            if (section != null)
            {
                var dxStudioLocation = section.Settings["DxStudioLocation"].Value;
            }
        }

You can globalize AppSettingsSection with some minor adjustments:

<section name="fileEnvironment" type="System.Configuration.AppSettingsSection"/>

Consume with:

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);

        if (!appConfiguration.HasFile) // no need to null check, ConfigurationManager.OpenMappedExeConfiguration will always return an object or throw ArgumentException
        {
            //Configuration file not found, so throw an exception
        }
        else
        {
            var section = appConfiguration.GetSection("fileEnvironment") as AppSettingsSection;
            if (section != null)
            {
                var dxStudioLocation = section.Settings["DxStudioLocation"].Value;
            }
        }
指尖上得阳光 2025-01-04 15:10:58

在.net中,配置文件是由正在运行的exe文件选择的,因此,如果您有5个项目(4个dll和1个exe),并且每个项目都有不同的配置文件,那么当您从exe文件运行应用程序时,dll的文件是被他加载的会认为exe的config文件是自己的config文件。

换句话说,要读取dll项目的配置文件,您需要使用他的路径显式打开它。

希望有帮助

In .net the config file is chosen by the running exe file, so if you have 5 projects (4 dll's and one exe), and each project has a different config file when you will run your app from the exe file the dll's which are loaded by him will think that the config file of the exe is theirs config file.

in other words, to read the config file of the dll project you need to open it explicitly using his path.

hope it helps

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