当前位置: 移动技术网 > IT编程>开发语言>c# > C#基础知识之FileStream

C#基础知识之FileStream

2019年07月18日  | 移动技术网IT编程  | 我要评论

一、filestream的基础知识

  属性:

           canread 判断当前流是否支持读取,返回bool值,true表示可以读取
           canwrite 判断当前流是否支持写入,返回bool值,true表示可以写入

  方法:

           read() 从流中读取数据,返回字节数组
           write() 将字节块(字节数组)写入该流
           seek() 设置文件读取或写入的起始位置
           flush() 清除该流缓冲区,使得所有缓冲的数据都被写入到文件中
           close() 关闭当前流并释放与之相关联的所有系统资源

  文件的访问方式:(fileaccess)

           fileaccess.read(对文件读访问)
           fileaccess.write(对文件进行写操作)
           fileaccess.readwrite(对文件读或写操作)

  文件打开模式:(filemode)包括6个枚举

          filemode.append 打开现有文件准备向文件追加数据,只能同fileaccess.write一起使用
          filemode.create 指示操作系统应创建新文件,如果文件已经存在,它将被覆盖
          filemode.createnew 指示操作系统应创建新文件,如果文件已经存在,将引发异常
          filemode.open 指示操作系统应打开现有文件,打开的能力取决于fileaccess所指定的值
          filemode.openorcreate 指示操作系统应打开文件,如果文件不存在则创建新文件
          filemode.truncate 指示操作系统应打开现有文件,并且清空文件内容

  文件共享方式:(fileshare)

         fileshare方式是为了避免几个程序同时访问同一个文件会造成异常的情况。

  文件共享方式包括四个:

        fileshare.none 谢绝共享当前文件
        fileshare.read 充许别的程序读取当前文件
        fileshare.write 充许别的程序写当前文件
        fileshare.readwrite 充许别的程序读写当前文

二、filestream的异步操作

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
using system.io;
using system.threading;

namespace streamwin
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }

    private void button1_click(object sender, eventargs e)
    {
      string filepaths = @"e:\test\test\local\a.txt";
      string filename ="a.txt" ;

      system.io.fileinfo f = new fileinfo(@"e:\test\test\server\a.txt");
      int filelength = convert.toint32(f.length.tostring());

      threadpool.setmaxthreads(100, 100);
      using (system.io.filestream stream = new system.io.filestream(filepaths, filemode.create,fileaccess.write, fileshare.write, 1024, true))
      {
        for (int i = 0; i < filelength; i +=100 * 1024)
        { 
          int length = (int)math.min(100 * 1024, filelength - i);
          var bytes = getfile(filename, i, length);
          stream.beginwrite(bytes, 0, length, new asynccallback(callback), stream);
        }
        stream.flush();
      }
    }

    public static byte[] getfile(string name, int start, int length)
    {
      string filepath = @"e:\test\test\server\a.txt";
      using (system.io.filestream fs = new system.io.filestream(filepath, system.io.filemode.open, system.io.fileaccess.read, fileshare.readwrite,1024,true))
      {
        byte[] buffer = new byte[length];
        fs.position = start;
        fs.beginread(buffer, 0, length,new asynccallback(completed),fs);
        return buffer;
      }
    }

    static void completed(iasyncresult result)
    {
      filestream fs = (filestream)result.asyncstate;
      fs.endread(result);
      fs.close();
    }
    public static void callback(iasyncresult result)
    {
      filestream stream = (filestream)result.asyncstate;
      stream.endwrite(result);
      stream.close();
    }
  }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网