Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修改应用程序设置时性能不佳_C#_Performance_Configuration_App Config_Appsettings - Fatal编程技术网

C# 修改应用程序设置时性能不佳

C# 修改应用程序设置时性能不佳,c#,performance,configuration,app-config,appsettings,C#,Performance,Configuration,App Config,Appsettings,我有一个应用程序,其中我从数据库的值创建一个树视图,并用复选框显示它。 现在我想将所选值写入appSettings。我试着用这个代码来做这件事。 但是性能太差了,这可能不是正确的方法。 我怎样才能更好地解决它 public static void SearchAndSaveSelectedNodes(TreeNodeCollection nodes) { foreach (TreeNode n in nodes) { DeleteSetting(n.Name);

我有一个应用程序,其中我从数据库的值创建一个树视图,并用复选框显示它。 现在我想将所选值写入appSettings。我试着用这个代码来做这件事。 但是性能太差了,这可能不是正确的方法。 我怎样才能更好地解决它

public static void SearchAndSaveSelectedNodes(TreeNodeCollection nodes)
{
    foreach (TreeNode n in nodes)
    {
        DeleteSetting(n.Name);

        if (n.Checked)
        {
            UpdateSetting(n.Name, n.Name + "@" + n.FullPath);
        }
        SearchAndSaveSelectedNodes(n.Nodes);
    }
}

public static void DeleteSetting(string key)
{
    System.Configuration.Configuration config = `ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);`
    config.AppSettings.Settings.Remove(key);
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
}

public static void UpdateSetting(string key, string value)
{
    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings.Remove(key);
    config.AppSettings.Settings.Add(key, value);
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
}

我认为问题在于您打开/保存/刷新了。。等。多次重复配置

我已经快速“内联”了所需的调用,因此它们只在需要时执行,并添加了第二个方法来进行递归调用,而无需重复打开/保存。(未经测试)

检查一下这是否适合你

public static void SearchAndSaveSelectedNodes(TreeNodeCollection nodes)
{
    // open config (only once)
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    // make edits (recursive)
    SearchAndSaveSelectedNodesRecursive(nodes, config);

    // save (only once)
    config.Save(ConfigurationSaveMode.Modified);
    // (afaik there is no need to refresh the section)
}

private static void SearchAndSaveSelectedNodesRecursive(TreeNodeCollection nodes, Configuration config)
{
    foreach (TreeNode n in nodes)
    {
        config.AppSettings.Settings.Remove(n.Name);
        if (n.Checked)
        {
            // no need to delete again here (it's already deleted)
            config.AppSettings.Settings.Add(n.Name, n.Name + "@" + n.FullPath);
        }
        SearchAndSaveSelectedNodesRecursive(n.Nodes, config);
    }
}

与任何文件处理一样,您需要打开文件一次,进行所有更改,然后保存文件一次。按照您构建它的方式,将为每个条目打开文件,写入条目,然后保存文件


想象一下,你必须在编辑器中键入这些内容,然后在每行之后打开和关闭编辑器,而不是打开一次,全部键入然后保存一次。

为什么不将其序列化到另一个文件中呢?有两件事。第一:如果更新一个条目,您总是删除两次(删除设置,然后更新设置,这也会删除)。第二:您可以分别为每个节点打开和写入该文件,为什么不一步更新它呢?这里有多少节点是相关的?