2012-01-16 58 views
-2

我有一些数据(串)的列表:已经从内存中的列表数据的程序关闭,即使

Anna 
Amy 
Johnny 
John 

然后我有一个从列表中获取数据,并将其写入到一个方法列表显示。

但是,当我关闭表单中的所有数据消失(这是显而易见的),所以我不知道如何顺利​​记住列表中的数据,以便下次运行该程序时,程序会记住数据直接将列表中的项目写入ListView。

+1

FILES如何? – Apurv 2012-01-16 08:45:57

+0

你的意思是把它写入文件? – user1151471 2012-01-16 08:46:36

+0

Ofcourse,写入文件,然后读回 – Apurv 2012-01-16 08:48:08

回答

0

您需要将数据保存在某个地方。

在降低复杂性,我建议......

  1. 关系数据库(http://msdn.microsoft.com/en-us/library/ms233816(v=vs.80).aspx )
  2. 本地保存到文件(XML例如http://support.microsoft.com/kb/815813
  3. 写入文本文件(http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx)

从链接(3 )这显示了您可以轻松读取和写入文件

namespace csharp_station.howto 
{ 
class TextFileWriter 
{ 
    static void Main(string[] args) 
    { 
     // create a writer and open the file 
     TextWriter tw = new StreamWriter("date.txt"); 

     // write a line of text to the file 
     tw.WriteLine(DateTime.Now); 

     // close the stream 
     tw.Close(); 
    } 
} 
} 



class TextFileReader 
{ 
    static void Main(string[] args) 
    { 
     // create reader & open file 
     Textreader tr = new StreamReader("date.txt"); 

     // read a line of text 
     Console.WriteLine(tr.ReadLine()); 

     // close the stream 
     tr.Close(); 
    } 
} 
0

假设你希望将数据保留在应用程序可执行文件,准备已退出,因为当你下一次运行它,那么你就需要在一个永久的存储解决方案,例如看后数据库或文件。即使NET分区允许你,你也不能将数据驻留在内存中,你不知道在哪里寻找它。

如果您希望在关闭表单后仍然可以使用数据,但仍然运行该应用程序,那么您需要使数据在更高级别上可用,有问题的表单退出。

0

对于像这样的小型应用程序特定数据,我建议使用内置于持久设置中的C#。这些将保存到应用程序配置文件。

您可以如下访问它们:

// Access the property 
Properties.Settings.Default.SettingName = "I'm a string"; 

// Make sure to save before you exit! Add this to your main form closing event handler perhaps? 
Properties.Settings.Default.Save(); 

你需要将它们添加到设置文件第一,但。在Visual Studio中很容易。在解决方案资源管理器中:Project - > Preferences - > Settings.settings。

相关问题