您的位置:程序门 -> .net技术 -> c#



如何把combobox的内容保存在.txt文件中?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何把combobox的内容保存在.txt文件中?
发表于:2007-03-14 13:38:22 楼主
如果保存了
那又如何从.txt文件中加载呢?
发表于:2007-03-14 13:47:311楼 得分:0
writing   to   a   text   file
listing   1:   writing   text   data   to   a   file:   textfilewriter.cs

using   system;
using   system.io;

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();
                }
        }
}


reading   from   a   text   file

listing   2   shows   how   to   read   from   a   text   file:
listing   2:   reading   text   data   from   a   file:   textfilereader.cs

using   system;
using   system.io;

namespace   csharp_station.howto
{
        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();
                }
        }
}
发表于:2007-03-14 14:21:492楼 得分:0
//载入
streamreader   vstreamreader   =   new   streamreader(
        @ "c:\temp\temp.txt ",   encoding.default);
combobox1.items.clear();
string   vline;
while   ((vline   =   vstreamreader.readline())   !=   null)
{
        combobox1.items.add(vline);
}
vstreamreader.close();
发表于:2007-03-14 14:23:573楼 得分:0
//保存
streamwriter   vstreamwriter   =   new   streamwriter(
        @ "c:\temp\temp.txt ",   false,   encoding.default);
foreach   (string   vline   in   combobox1.items)
        vstreamwriter.writeline(vline);
vstreamwriter.close();


快速检索

最新资讯
热门点击