【发布时间】:2013-10-29 04:08:21
【问题描述】:
如何在 c# 中将 ArrayList 中的所有内容保存到 .txt 文件中,然后在启动 WPF 应用程序时加载它?
【问题讨论】:
-
到目前为止你有没有尝试过?和
ArrayList2013 年?请改用List<T>。 -
根据 Soner 的评论,使用强类型集合为您提供了自 1.1 版以来内置于 .net 的序列化
如何在 c# 中将 ArrayList 中的所有内容保存到 .txt 文件中,然后在启动 WPF 应用程序时加载它?
【问题讨论】:
ArrayList 2013 年?请改用List<T>。
static void SaveArray()
{
ArrayList myArray = new ArrayList();
myArray.Add("First");
myArray.Add("Second");
myArray.Add("Third");
myArray.Add("and more");
StreamWriter sw= File.CreateText(@"C:\file.txt");
foreach (string item in myArray)
{
sw.WriteLine(item);
}
sw.Close();
}
你不应该使用 arraylist 不是因为它是 2013 年,而是因为 arraylist 将数组中的每个项目装箱,其中 List 也存储类型。
这会减少内存使用成本。
【讨论】: