ConfigurationManager Save() wants to create a tmp-File

落爺英雄遲暮 提交于 2019-12-07 05:23:54

问题


I am having problems with a application that wants to write to the .exe.config.

See following code:

using System;
using System.Configuration;
using System.IO;

namespace ConfigurationManagerError
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // set Config-File to persistent folder
                ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
                exeMap.ExeConfigFilename = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                        "ConfigError\\ConfigurationManagerError.exe.config");

                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);    

                config.AppSettings.Settings.Add(
                    new KeyValueConfigurationElement("TestKey", "TestValue"));

                config.Save(ConfigurationSaveMode.Modified);
                Console.WriteLine("Successfully write of Configuration-File to " + config.FilePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.Read();
        }
    }
}

As long as I run as a user with read and write access on the folder everything works. If I have a user that has no write permission in the folder there is a exception that says that permission to write the .exe.config is not allowed. Until now everything is as expected.

But if I now have a user that has the right to write to existing files but not to create new files an exception is thrown with something like

Access to the path 'C:\ProgramData\ConfigError\ila2ba_0.tmp' is denied

It seems that ConfigurationManager wants to create a tmp-File. How can this be solved?

Thanks a lot! Best regards, Joerg


回答1:


A user specific configuration setting which need to be rewritten multiple times should be saved under user's application data directory.

You can have a generic application configuration installed along with your application which can be merged with user specific configuration and required config setting can be overridden as per your requirement.



来源:https://stackoverflow.com/questions/24635071/configurationmanager-save-wants-to-create-a-tmp-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!