在设置中存储密钥阵列
问题描述:
我有一个名为“KeyboardInput”的密钥数组。我怎样才能将其存储在设置,然后又读出来作为单独的按键有点像:在设置中存储密钥阵列
for (int i = 0; i < KeyboardInput.Length; i++)
{
KeyboardInput[i] = (Keys)Enum.Parse(typeof(Keys), {{ Properties.Settings.Default.Keys[i]?? }});
}
Keys[] KeyboardInput = { Keys.A, Keys.S, Keys.N, Keys.M, Keys.H, Keys.F, Keys.T, Keys.G, Keys.W, Keys.Q, Keys.Z, Keys.X, Keys.Right, Keys.Left, Keys.Up, Keys.Down };
答
令人惊讶的前Related question答案似乎工作。添加设置后,右键单击Settings.settings在Solution Explorer中,打开...任何文本编辑器,并更改类型:
<Setting Name="Keys" Type="System.Windows.Forms.Keys[]" Scope="User">
这也将更新Settings.Designer.cs
文件,然后你可以使用它是这样的:
Keys[] KeyboardInput = { Keys.A, Keys.S, Keys.N, Keys.M, Keys.H, Keys.F, Keys.T, Keys.G,
Keys.W, Keys.Q, Keys.Z, Keys.X, Keys.Right, Keys.Left, Keys.Up, Keys.Down };
Properties.Settings.Default.Keys = KeyboardInput;
Properties.Settings.Default.Save();
Keys[] keys = Properties.Settings.Default.Keys;
+0
我通过做.split(',')来读取它,结束了使用原来的评论。 – PRAGMA
Settings以字符串格式(XML)存储,因此您可以将它们全部加入到一个字符串中,然后拆分存储的字符串并进行解析。另一种方法是查看XML或JSON序列化。 – Slai
@EZI在我的情况下似乎没有帮助 – PRAGMA
@PRAGMA您想保存一些设置并在稍后阅读,我错过了什么? –