欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#简单读取文本的实例方法

程序员文章站 2023-12-11 18:49:40
复制代码 代码如下:using system;using system.collections.generic;using system.linq;using system...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;

namespace streamreadwrite
{
    class program
    {
        static void main(string[] args)
        {
            // get the directories currently on the c drive.
            directoryinfo[] cdirs = new directoryinfo(@"e:\").getdirectories();

            // write each directory name to a file.
            using (streamwriter sw = new streamwriter("cdrivedirs.txt"))
            {
                foreach (directoryinfo dir in cdirs)
                {
                    sw.writeline(dir.name);

                }
            }

            // read and show each line from the file.
            string line = "";
            using (streamreader sr = new streamreader("cdrivedirs.txt"))
            {
                while ((line = sr.readline()) != null)
                {
                    console.writeline(line);
                }
            }
        }
    }
}   

上一篇:

下一篇: