“设置”中的字符串集合不会更改其大小

时间:2016-12-13 20:22:08

标签: c# resize settings stringcollection

我的C#项目中有几个字符串集合存储在“设置”中。在这个项目的开头,我初始化了这些集合,包含20个值,都是0。最近我意识到我实际上每个集合中需要大约100个0,所以我去了每个集合再添加80个。这工作正常,但现在我看到了奇怪的事情。

我得到了一个超出范围的异常,当我跟着它时,它试图在我的一个字符串集合中访问索引为20的项目。嗯。那应该不是这样的,所以我去检查我的stringCollection,它显然太小而无法处理20的索引,而且确实还是我的所有100个仍然存在。那么在断点处我将鼠标悬停在我的字符串集合上,它显示.count = 20.什么?我可以在settings.settings文件中看到100 0。我算好了吗?

然后我去删除了那个字符串集合,创建了一个带有SAME名称和相同内容的新字符串,它没问题。但是我不想在我的设置中为每个字符串集合重做这个。

有没有人碰到过这个?这是一个错误吗?为什么如果我删除该集合然后重新制作它,它会突然起作用?

编辑:我的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="OPCServer_Simulator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup><userSettings>
        <OPCServer_Simulator.Properties.Settings>
            <setting name="portName" serializeAs="String">
                <value>COM1</value>
            </setting>
            <setting name="numItems" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="recipeCode" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="nomThick" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="nomWidth" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="meas9" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting> //you get the idea
        </OPCServer_Simulator.Properties.Settings>
    </userSettings>
</configuration>

编辑2:我的for循环导致他出现问题

for (int k = 0; k <= 99; k++)
            {
                for (int i = 1; i <= 20; i++) //adding the specified number of items to OPC server
                {
                    var value = Properties.Settings.Default["meas" + i] as System.Collections.Specialized.StringCollection;
                    value[k] = "0";
                }
                var dataSet = Properties.Settings.Default["dateTime"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = " ";
                dataSet = Properties.Settings.Default["recipeCode"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
                dataSet = Properties.Settings.Default["nomWidth"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
                dataSet = Properties.Settings.Default["nomThick"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
            }

0 个答案:

没有答案