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

发布于 2024-09-07 18:50:14 字数 1175 浏览 8 评论 0原文

我有一个程序,它使用 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;
    }

问题是:有没有办法提升此操作的权限?或者还有什么办法可以解决这个问题?

谢谢你!

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!

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

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

发布评论

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

评论(3

我不是你的备胎 2024-09-14 18:50:15

按原样回答你的问题,没有办法“暂时”提升。该应用程序必须通过海拔请求启动。

通常,如果应用程序通常不需要提升,您可以将需要提升的功能分解为由开关控制的模式,然后使用 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 目录中,从而不需要提升。

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.

汐鸠 2024-09-14 18:50:15

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

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.

草莓味的萝莉 2024-09-14 18:50:14

全局 app.config 文件(对于 EXE)通常不应该由使用它们的程序进行编辑 - 更常见的是由安装程序等进行编辑。您可能想要的只是用户设置(更改范围)。它们通常存储在 AppData 中,更好的是,设置的代理属性是自动生成的,因此您可以执行以下操作:

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

这(几乎)是我在管理设置!

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!

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