如何获得在C#中编辑app.config的管理员权限?

如何获得在C#中编辑app.config的管理员权限?,c#,app-config,privileges,C#,App Config,Privileges,我有一个程序,它使用app.config来存储一些首选项。问题是,如果程序安装在C:\program files\中,则由于program files\中的所有文件仅对管理员可用,因此无法更改首选项 我的代码: public static bool EditKeyPair(string key, string value) { bool success = true; // Open App.Config of executable

我有一个程序,它使用app.config来存储一些首选项。问题是,如果程序安装在C:\program files\中,则由于program files\中的所有文件仅对管理员可用,因此无法更改首选项

我的代码:

    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;
    }
问题是:有没有办法提升此操作的权限?或者如何解决这个问题


谢谢大家!

在项目属性中,将这些设置从Scope=Application更改为Scope=User。这样,它们就存储在用户的配置文件中,您不需要管理员权限来修改它们。

在项目属性中,将这些设置从Scope=Application更改为Scope=user。这样,它们就存储在用户的配置文件中,您不需要管理员权限来修改它们。

Global app.config文件(用于EXE)通常不会由使用它们的程序进行编辑,更常见的是由安装程序等进行编辑。你可能想要的只是(一个简单的改变范围的问题)。它们通常存储在
AppData
中,更妙的是,设置的代理属性是自动生成的,因此您可以执行以下操作:

Properties.Settings.Default.Foo = "bar";
Properties.Settings.Default.Save();
这(几乎)是我在管理设置时需要做的所有事情

Global app.config文件(用于EXE)通常不由使用它们的程序进行编辑,更典型的是由安装程序等进行编辑。你可能想要的只是(一个简单的改变范围的问题)。它们通常存储在
AppData
中,更妙的是,设置的代理属性是自动生成的,因此您可以执行以下操作:

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

这(几乎)是我在管理设置时需要做的所有事情

按照现状回答您的问题,没有“暂时”提升的方法。必须使用提升请求启动应用程序

通常,如果一个应用程序通常不需要提升,那么您可以将需要提升的功能分解为一个由开关控制的模式,然后使用runas动词启动自己

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;
}

但是,您可能希望放置用户可能希望在用户范围内修改的配置设置,以便配置驻留在其
appData
目录中,因此不需要提升。

要回答您的问题,实际上,没有“临时”提升的方法。必须使用提升请求启动应用程序

通常,如果一个应用程序通常不需要提升,那么您可以将需要提升的功能分解为一个由开关控制的模式,然后使用runas动词启动自己

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;
}

但是,您可能希望放置用户可能希望在用户范围内修改的配置设置,以便配置位于其
appData
目录中,因此不需要提升。

谢谢!评论在哪里?我认为汤姆是对的。我应该找到app.config的真正用途。相反,我只是猜错了。代码似乎是:Properties.Settings.Default.variable=value。注意:设置属性例如:Foo是通过双击项目中的Settings.setting文件创建的。谢谢!评论在哪里?我认为汤姆是对的。我应该找到app.config的真正用途。相反,我只是猜错了。代码似乎是:Properties.Settings.Default.variable=value。注意:设置属性例如:Foo是通过双击项目中的Settings.setting文件创建的。