我正在检查 IF File EXIST。
if (File.Exists("Accounts.txt"))
{
string jsonToRead = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Accounts.txt"));
vals =
JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(jsonToRead);
}
然后我添加我的帐户
account = Account_No;
vals.Add(new KeyValuePair<string, string>(Bank_Names, account));
AccountsComboBox2.Items.Add(account);
和保存帐户
using (StreamWriter sw = File.CreateText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Accounts.txt")))
{
string json = JsonConvert.SerializeObject(vals);
sw.Write(json);
}
当应用程序重新加载时,我希望帐户显示在Treeview中。
我试过这样做但没有帮助。
if (File.Exists("Accounts.txt"))
{
string treeView = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Accounts.txt"));
// ACCOUNTS.TXT FILE与keyvaluepair一起保存,我想将KeyValuePair转换为TreeView,其中KEY是父节点,Value是它的子节点。
vals =
JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(TreeView);
TreeNode tree = (TreeNode)vals;
}
如果数据不在树视图中,我如何重新填充Treeview?
答案是:
父节点位于bank comboBox中,我在运行时动态添加到Treeview。
string[] items = new string[BankList.Items.Count];
for (int i = 0; i < BankList.Items.Count; i++)
{
items[i] = BankList.Items[i].ToString();
node = treeView1.Nodes.Add(items[i]);
}
当我添加新帐户时,我将数据保存到键值对以及在树状视图中显示
像这样:
TreeNode newNode = new TreeNode(AccountNotextBox1.Text);
//this is name
newNode.Name = AccountNotextBox1.Text;
//this is tag
newNode.Tag = AccountNotextBox1.Text;
treeView1.Nodes[BankList.SelectedIndex].Nodes.Add(newNode);
我将数据保存在Accounts.txt中的keyvalue对中,并将树视图状态保存在单独的文件名中&#34; Tree.txt&#34;在表单关闭我写了这段代码。
private void AccountsSetup_FormClosing(object sender, FormClosingEventArgs e)
{
SaveTree(treeView1, "Tree.txt");
}
帐户表单加载
LoadTree(treeView1, "tree.txt");
银行及其帐户数据正在保存在键值对文件名accounts.txt中,并在应用启动时重新加载。
添加新帐户后,和treeview状态将保存在tree.txt中。
我必须将其保存在另外一个单独的文件中。
我的问题是为什么我不能将KeyValuePair转换回TreeNode 并使用已保存的数据而不是保存树状态 在用户计算机上的文本文件,它不是那么专业,无论是 状态应该在应用程序设置或用户的某个位置 如果这个文件,则不必担心备份treeview 被删除。
组合框将显示保存在Accounts.txt中的keyValuePair数据。
但是树视图将为空,仅显示动态银行列表作为父节点。
让我们说&#34; Tree.txt&#34;文件被删除,这就会发生。 树状态消失了,但那里有帐户。