如何在C#中将treeview内容保存到txt文件中 [英] How to save treeview content into a txt file in C#

查看:290
本文介绍了如何在C#中将treeview内容保存到txt文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我想将Tree-view内容保存到TXT文件。

将它保存到txt的最简单方法是什么?



我的尝试:



Hi
I would like to save the Tree-view content to a TXT file.
What is the easiest way to save it to txt?

What I have tried:

treeViewIn.Nodes.ToString();

TextWriter tw = new StreamWriter(@"InOrder.txt");
IList list = treeViewIn.Nodes;
     for (int i = 0; i < list.Count; i++)
          {
              string s = (string)list[i];
              tw.WriteLine(s);
          }
     tw.Close();

推荐答案

这里有两个问题:第一个是a treeview不是一个平面显示器,它可以有分支,分支可以有树枝:

There are two problems here: the first is that a treeview is not a "flat" display, it can have branches, and the branches can have twigs:
A
|\
| B
| |\
| | C
| D
|  \
|   E
F

因此,简单的循环不是访问所有节点进行存储的实用方法。这是一个递归数据结构,所以你需要考虑一个递归方法才能正确访问它。



第二个是更大的负载:一个平坦的数据文件,如文本文档不适合递归数据!它很容易存储,但是你如何在以后阅读它以重建原始的基于树的视图?如果没有这种能力,你就会浪费时间存储数据:如果你不能读到它的重点是什么?



我建议的是您将看到三种存储方法之一:XML数据,JSON数据和关系数据库。它们都有优点和缺点,您应该选择哪一个取决于您打算如何使用数据以及存储后您需要做什么。



但说实话,你可能是错误的做法:根据你的显示方式设计你的数据存储几乎肯定是一个错误;数据非常重要,不能考虑到最后!

So a simple loop is not a practical way to access all the nodes for storage. It's a recursive data structure, so you need to think about a recursive method to access it all correctly.

The second is a load bigger: a flat data file like a text document is not a good "fit" for recursive data! It's easy to store, but how do you read it back later to reconstruct the original tree based view? And without that ability, you are wasting your time storing the data at all: if you cant read it where is the point?

What I';d suggest is that you look at one of three storage methods: XML data, JSON data, and a relational database. They all have advantages and disadvantages, and which one you should probably chose will depend on how you intend to use the data and what you ne3ed to do with it once you have stored it.

But to be honest, you are probably doing this the wrong way round: designing your data storage based on how you are displaying it is almost certainly a mistake; data is far too important to leave to consider last!


这篇关于如何在C#中将treeview内容保存到txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆