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

c#文件操作示例带详细注释

程序员文章站 2024-02-25 11:32:40
复制代码 代码如下:using system; using system.collections.generic; using system.linq; using sys...

复制代码 代码如下:

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

namespace example
{

    class program
    {

        static void main(string[] args)
        {
            ////////////////   文件打开  下面的代码打开d:\wang.txt文件,并且向文件中写入"hello"
            filestream textfile = file.open(@"d:\wang.txt", filemode.append);//以append方式打开文件(如果不存在,会创建)
            byte[] info = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o' };//要写入的信息
            textfile.write(info, 0, info.length);//write方法只能写入byte数组                       
            textfile.close();//关闭文件流
          

            ////////////////////// 文件创建   
            filestream newtext = file.create(@"d:\newtext.txt");//创建文件
            newtext.close();//关闭文件

            ////////////////////  删除文件
            file.delete(@"d:\newtext.txt");

            //////////////////  文件复制   如果目标文件存在,不允许复制(就是不能覆盖同名文件)
            //file.copy(@"d:\wang.txt", @"d:\copywang.txt");


            ////////////////  文件移动  只能在同一个盘中移动  如果目标路径不正确,不能移动
           // file.move(@"d:\copywang.txt", @"d:\a\movewang.txt");

            ////////////////////////  设置文件属性为 只读,隐藏
            //file.setattributes(@"d:\copywang.txt", fileattributes.readonly | fileattributes.hidden);//同时满足多个属性,必须用位或(|).

            ///////////////  判断文件是不是存在
            if (file.exists(@"d:\copywang.txt"))//如果存在  即便是隐藏的文件也可以找到
            {
                file.setattributes(@"d:\copywang.txt", fileattributes.readonly);//重新设置属性后,隐藏的文件也会显示出来,只要不加hidden属性
                console.writeline("找到文件copywang.txt");
            }
            else
            {
                console.writeline("没有找到文件copywang.txt");
            }
            /*
            此外,file类对于text文本提供了更多的支持。
          ?appendtext:将文本追加到现有文件
          ?createtext:为写入文本创建或打开新文件
          ?opentext:打开现有文本文件以进行读取
          但上述方法主要对utf-8的编码文本进行操作,从而显得不够灵活。在这里推荐读者使用下面的代码对txt文件进行操作。
          ?对txt文件进行“读”操作,示例代码如下:  
             */
            streamreader textreader = new streamreader(@"d:\wang.txt", system.text.encoding.default);//以默认编码方式打开文件
            string str = textreader.readtoend();//读取文件
            console.writeline("使用streamreader读取文本内容:" + str);
            textreader.close();

            //////////////////对txt文件写内容
            streamwriter textwriter = new streamwriter(@"d:\wang.txt");
            str = "learn .net";
            textwriter.write(str);
            textwriter.close();

            /*
            system.io.directory类和system.directoryinfo类
           主要提供关于目录的各种操作,使用时需要引用system.io命名空间。下面通过程序实例来介绍其主要属性和方法。
            */
            directory.createdirectory(@"d:\wang1\wang");//创建目录(文件夹)如果已经存在,则保持;还可以一次创建多级目录

            /////////////////////////////////目录属性设置方法
            directoryinfo dirinfo = new directoryinfo(@"d:\wang1\wang");//
            dirinfo.attributes = fileattributes.hidden;// | fileattributes.readonly;//设置文件夹属性

            /////////////////delete方法的第二个参数为bool类型,它可以决定是否删除非空目录。
            //如果该参数值为true,将删除整个目录,即使该目录下有文件或子目录;若为false,则仅当目录为空时才可删除。
            //directory.delete(@"d:\wang1", true);//如果文件设置为readonly,则不能删除

            //directory.move(@"d:\wang1", @"d:\wang3");//把文件夹wang1移动到文件夹wang3中,相当于把wang1删除,创建一个wang3,再把内容移动到wang3

            string[] directories = directory.getdirectories(@"d:\wang3");//获得文件夹wang3的目录
            foreach (string var in directories)
                console.writeline(var);

            string[] files = directory.getfiles(@"d:\wang1");//获取文件夹wang1下面的所有文件
            foreach (string var in files)
                console.writeline(var);

            if (directory.exists(@"d:\wang1"))
                console.writeline("文件夹wang1存在");

            /*
            在c#中 “\”是特殊字符,要表示它的话需要使用“\\”。由于这种写法不方便,c#语言提供了@对其简化。只要在字符串前加上@即可直接使用“\”。
                所以上面的路径在c#中应该表示为“book”,@“\tmp\book”,@“c:\tmp\book”。
            */

            console.readline();  

        }
    }
}