Advertisement
iljimae

Save/Load ListBox/Read file text C#

Jun 22nd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. //load
  2. {
  3. this.listBox1.Items.Clear();
  4. OpenFileDialog Open = new OpenFileDialog();
  5. Open.Filter = "Text Document|*.txt|All Files|*.*";
  6. try
  7. {
  8. Open.ShowDialog();
  9. StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
  10. while (Import.Peek() >= 0)
  11. listBox1.Items.Add(Convert.ToString(Import.ReadLin e()));
  12. }
  13. catch (Exception ex)
  14. {
  15. MessageBox.Show(Convert.ToString(ex.Message));
  16. return;
  17. }
  18. }
  19.  
  20. //save
  21. StreamWriter Write;
  22. SaveFileDialog Open = new SaveFileDialog();
  23. try
  24. {
  25. Open.Filter = ("Text Document|*.txt|All Files|*.*");
  26. Open.ShowDialog();
  27. Write = new StreamWriter(Open.FileName);
  28. for (int I = 0; I < listBox4.Items.Count; I++)
  29. {
  30. Write.WriteLine(Convert.ToString(listBox4.Items[i]));
  31. }
  32. Write.Close();
  33. }
  34. catch (Exception ex)
  35. {
  36. MessageBox.Show(Convert.ToString(ex.Message));
  37. return;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement