如何将列表框项目保存为.config 文件并将其读回列表框?
how to save listbox item as .config file and read it back to listbox?
在我的 WPF 项目中,我有一个列表框,我可以通过这种方式向其中添加一些项目:
//item is a class type of Product
var item = GetProductByID(ProductID);
lb_Configuration.Items.Add(item);
现在我想在关闭此应用程序时将列表框项目保存为配置文件,当我重新打开它时,我可以将此配置文件重新加载到应用程序,从而将相应的项目添加到列表框,我应该怎么想去做这个?提前致谢!
编辑:
private void OpenFile_Executed(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Multiselect = true;
openFile.Title = "Please Choose Your File";
openFile.Filter = "All Files(*,*)|*.*";
if (openFile.ShowDialog() == true)
{
/* What to do after open file */
StreamReader sr = File.OpenText(openFile.FileName);
while (sr.EndOfStream != true) ;
}
}
private void SaveFile_Executed(object sender, ExecutedRoutedEventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Text File|*.txt";
if (saveFile.ShowDialog() == true)
{
StreamWriter sw = File.AppendText(saveFile.FileName);
sw.Flush();
sw.Close();
}
}
无法给出确切的工作答案,因为我们不知道类型 Product
的构成,但这里的底线是序列化 - 这可能非常简单,或者可以得到更复杂取决于类型 Product
使用和公开的类型。为此有许多工具:JSON.net、ServiceStack,甚至内置的 .net 序列化。
对于简单的例子,考虑使用JavaScriptSerializer:
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(item);
然后从另一个方向做...
var item = serializer.Deserialize<Product>(json);
这是一个起点,如果没有别的。显然调整它以序列化整个集合,确保所有相关值都被正确地序列化和反序列化,并保存到文件:
File.WriteAllText(pathToFile, json);
回读:
var json = File.ReadAllText(pathToFile);
使用应用目录下的txt文件:
public partial class MainWindow : Window
{
private string fileName = "lst.txt";
private List<string> lst;
public MainWindow()
{
InitializeComponent();
this.Closing += (s, e) =>
{
File.WriteAllLines(fileName, this.lst);
};
}
//in other events, button click, initialize or change the lst
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
this.lst = File.ReadAllLines(fileName).ToList();
}
catch (Exception)
{
}
}
//simulate your other changes to the lst
private void Button_Click(object sender, RoutedEventArgs e)
{
if (lst == null)
lst = new List<string>();
lst.Add(new Random().Next().ToString());
}
}
在我的 WPF 项目中,我有一个列表框,我可以通过这种方式向其中添加一些项目:
//item is a class type of Product
var item = GetProductByID(ProductID);
lb_Configuration.Items.Add(item);
现在我想在关闭此应用程序时将列表框项目保存为配置文件,当我重新打开它时,我可以将此配置文件重新加载到应用程序,从而将相应的项目添加到列表框,我应该怎么想去做这个?提前致谢!
编辑:
private void OpenFile_Executed(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Multiselect = true;
openFile.Title = "Please Choose Your File";
openFile.Filter = "All Files(*,*)|*.*";
if (openFile.ShowDialog() == true)
{
/* What to do after open file */
StreamReader sr = File.OpenText(openFile.FileName);
while (sr.EndOfStream != true) ;
}
}
private void SaveFile_Executed(object sender, ExecutedRoutedEventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Text File|*.txt";
if (saveFile.ShowDialog() == true)
{
StreamWriter sw = File.AppendText(saveFile.FileName);
sw.Flush();
sw.Close();
}
}
无法给出确切的工作答案,因为我们不知道类型 Product
的构成,但这里的底线是序列化 - 这可能非常简单,或者可以得到更复杂取决于类型 Product
使用和公开的类型。为此有许多工具:JSON.net、ServiceStack,甚至内置的 .net 序列化。
对于简单的例子,考虑使用JavaScriptSerializer:
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(item);
然后从另一个方向做...
var item = serializer.Deserialize<Product>(json);
这是一个起点,如果没有别的。显然调整它以序列化整个集合,确保所有相关值都被正确地序列化和反序列化,并保存到文件:
File.WriteAllText(pathToFile, json);
回读:
var json = File.ReadAllText(pathToFile);
使用应用目录下的txt文件:
public partial class MainWindow : Window
{
private string fileName = "lst.txt";
private List<string> lst;
public MainWindow()
{
InitializeComponent();
this.Closing += (s, e) =>
{
File.WriteAllLines(fileName, this.lst);
};
}
//in other events, button click, initialize or change the lst
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
this.lst = File.ReadAllLines(fileName).ToList();
}
catch (Exception)
{
}
}
//simulate your other changes to the lst
private void Button_Click(object sender, RoutedEventArgs e)
{
if (lst == null)
lst = new List<string>();
lst.Add(new Random().Next().ToString());
}
}