app.exe.config 中的更新值未反映在我的应用程序 .NET 中

发布于 2025-01-11 21:30:28 字数 606 浏览 0 评论 0原文

我确实无法理解为什么我的 app.exe 没有获取 app.exe.config 中的更新值

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<applicationSettings>
        <appName.AppProperties>
            <setting name="file_location" serializeAs="String">
                <value>C:\users\test\desktop\file_location.csv</value>
            </setting>
        </appName.AppProperties>
    </applicationSettings>
</configuration>

我认为一旦我重新启动应用程序,我的应用程序就会读取新位置(UAT,Prod) 的情况。 知道为什么 file_location 仍然相同(即缓存)吗?

I am really having propblem with understanding why my app.exe is not picking up the updated value in app.exe.config

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<applicationSettings>
        <appName.AppProperties>
            <setting name="file_location" serializeAs="String">
                <value>C:\users\test\desktop\file_location.csv</value>
            </setting>
        </appName.AppProperties>
    </applicationSettings>
</configuration>

I thought my application will read the new location once I restart the app in case of (UAT,Prod).
Any idea why the file_location is still the same aka cached?

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

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

发布评论

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

评论(1

往日情怀 2025-01-18 21:30:28

也许另一种方法是在 appSettings 下创建一个密钥。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <appSettings>
        <add key="file_location" value="C:\users\test\desktop\file_location.csv"/>
    </appSettings>
</configuration>

将以下类添加到您的项目中,该项目具有 file_location 的获取和设置。

using System;
using System.Configuration;
using System.IO;
using static System.Configuration.ConfigurationManager;

namespace YourNamespace
{
    public class ApplicationSettings
    {
        public static void SetFileLocation(string fileName) => SetValue("file_location", fileName);
        public static string GetFileLocation => AppSettings["file_location"];
        public static void SetValue(string key, string value)
        {

            var appName = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().Location);

            var configFile = Path.Combine(appName, 
                $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.exe.config");

            var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
            var config = OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
            {
                throw new Exception($"Key {key} does not exist");
            }

            config.AppSettings.Settings[key].Value = value;

            config.Save();

            RefreshSection("appSettings");

        }
    }
}

控制台应用测试

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            var current = ApplicationSettings.GetFileLocation;
            Console.WriteLine($"Current path: {current}");
            ApplicationSettings.SetFileLocation(current == "C:\\users\\test\\desktop\\file.csv" ?
                "C:\\users\\test\\desktop\\file_location.csv" :
                "C:\\users\\test\\desktop\\file.csv");

            Console.WriteLine($"Current path after set: {ApplicationSettings.GetFileLocation}");
            Console.ReadLine();
        }
    }
}

关于更新,请参阅以下内容

Perhaps an alternate approach, create a key under appSettings.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <appSettings>
        <add key="file_location" value="C:\users\test\desktop\file_location.csv"/>
    </appSettings>
</configuration>

Add the following class to your project which has a get and set for file_location.

using System;
using System.Configuration;
using System.IO;
using static System.Configuration.ConfigurationManager;

namespace YourNamespace
{
    public class ApplicationSettings
    {
        public static void SetFileLocation(string fileName) => SetValue("file_location", fileName);
        public static string GetFileLocation => AppSettings["file_location"];
        public static void SetValue(string key, string value)
        {

            var appName = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().Location);

            var configFile = Path.Combine(appName, 
                
quot;{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.exe.config");

            var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
            var config = OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
            {
                throw new Exception(
quot;Key {key} does not exist");
            }

            config.AppSettings.Settings[key].Value = value;

            config.Save();

            RefreshSection("appSettings");

        }
    }
}

Console app test

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            var current = ApplicationSettings.GetFileLocation;
            Console.WriteLine(
quot;Current path: {current}");
            ApplicationSettings.SetFileLocation(current == "C:\\users\\test\\desktop\\file.csv" ?
                "C:\\users\\test\\desktop\\file_location.csv" :
                "C:\\users\\test\\desktop\\file.csv");

            Console.WriteLine(
quot;Current path after set: {ApplicationSettings.GetFileLocation}");
            Console.ReadLine();
        }
    }
}

In regards to updating, see the following

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