How to get admin privileges for editing app.config in C#?

柔情痞子 提交于 2019-12-04 02:06:26

问题


I've got a program which uses app.config for storing some preferences. The problem is that if the program is installed in C:\program files\<project name> then the changing of preferences is not possible due to the fact that all files in program files\<project name> are available only to administrator.

My code:

    public static bool EditKeyPair(string key, string value)
    {
        bool success = true;

        // Open App.Config of executable
        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        KeyValueConfigurationCollection settings = config.AppSettings.Settings;

        // update SaveBeforeExit
        settings[key].Value = value;

        if (!config.AppSettings.SectionInformation.IsLocked)
        {
            //save the file
            config.Save(ConfigurationSaveMode.Modified);
            Debug.WriteLine("** Settings updated.");
        }
        else
        {
            Debug.WriteLine("** Could not update, section is locked.");
            success = false;
        }

        //reload the section you modified
        ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

        return success;
    }

The question is: Is there a way how to elevate privileges for this action? Or how else to solve the problem?

Thank you!


回答1:


Global app.config files (for EXEs) generally aren't meant to be edited by the program that uses them - more typically by installers and the like. What you probably want is simply User Settings (a simple matter of changing the scope). They generally get stored in AppData, and even better, proxy properties for the settings are auto-generated, so you can do something like:

Properties.Settings.Default.Foo = "bar";
Properties.Settings.Default.Save();

This is (pretty much) all I've ever needed to do when managing settings!




回答2:


To answer your question, as-is, there is no way to 'temporarily' elevate. The app must be launched with an elevation request.

Typically, if an app normally needs no elevation, you would break out the functionality that needs elevation into a mode controlled by a switch and then launch itself with a runas verb.

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = "-doSomethingThatRequiresElevationAndExit";
startInfo.Verb = "runas";
try
{
    Process p = Process.Start(startInfo);
    p.WaitForExit();

}
catch(System.ComponentModel.Win32Exception ex)
{
    return;
}

But, you might want to place config settings that a user may want to modify as user-scoped so that the config resides in their appData directory thus requiring no elevation.




回答3:


In the project properties, change those setting from being Scope=Application to Scope=User. That way, they're stored in the user's profile and you don't need administrator privileges to modify them.



来源:https://stackoverflow.com/questions/3147171/how-to-get-admin-privileges-for-editing-app-config-in-c

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