zoukankan      html  css  js  c++  java
  • C#窗体 流

    流:(I/O)输入输出流

    分类:文件流,内存流,网络流

    流的操作一般要放在try catch里面,操作文件网络容易出现异常

    命名空间:using system .IO;
    using system .Text; //二进制转换需要的命名空间

    一:类:FileStream:文件流

    1.构造:一般用第三个重载
    FileStream stream = new FileStream("d:\test.txt"(路径),FileMode.Creat(打开模式),FileAccess.Read(只读)); 
    第二个参数
    CreateNew 指定操作系统应创建新文件,如果文件存在则引发异常。

    Create 指定操作系统创建新文件,如果文件已存在则覆盖之。

    OPen 指定 操作系统应打开现有文件,如果文件不存在则抛出异常。

    OpenOrCreate 指定操作系统应打开文件,如果文件不存在则创建之。

    Truncate 指定操作系统打开现有文件,如果文件已存在则清空,从Truncate打开的文件中读取将引发异常。

    Append 若文件存在,则找到文件并找到文件结尾,或者创建一个新文件。
     
    第三个参数
    Read 对文件的读访问,拥有读取权限。

    Write 对文件的写访问,拥有写入权限。

    ReadWrite 对文件的读访问和写访问,拥有读取和写入两个权限。 


    写路径的时候前面加@,或者写双斜杠 @"d: est.txt"

    2.属性:

    Length:流的长度
    Position:流的当前位置,探测光标的当前位置

    3.方法

    Write(byte[]流的内容,int从第几个位置写,int写入长度);

    参数,第一个二进制数组,第二个从哪个位置开始写一般从0开始,第三个写进去多长(一般用Length,需要强转为int)。

    Read(byte[]存放读出流的空间,int从第几个位置读,int读多长);读文件

    seek(int偏移量,SeekOrigin.Begin(从哪计算偏移量))调整流的当前位置,seek(0,seekOrigin.End),将光标移到末尾

    Flush();清除缓存
    Close();关闭流

    4.用法:

    一:读文件:
    取出文件路径
    string path = openFileDialog1.FileName;

    打开文件流
    FileMode是打开模式 Open是打开文件 Append是追加 Create是创建 CreateNew创建新的
    OpenOrCreate有打开,没有创建 Truncate打开文件清空
    FileAccess 读或写
    FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read);

    造二进制数组,长度是流的长度
    byte[] bf = new byte[fs.Length];

    将文件中的数据读到二进制数组里面
    fs.Read(bf, 0, bf.Length);

    将而进制转码为字符串显示
    richTextBox1.Text = Encoding.Default.GetString(bf);

    关闭流
    fs.Close();

    二:写文件:
    取文件路径
    string path = saveFileDialog1.FileName;

    打开文件流
    FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write);

    将字符串转换为二进制数组
    byte[] nr = Encoding.Default.GetBytes(richTextBox1.Text);

    将而进制数组写入文件
    fs.Write(nr,0,nr.Length);

    关闭流
    fs.Close();

     


    StreamWrite类:

    取文件路径
    string path = saveFileDialog1.FileName;

    打开文件流
    FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write);

    第一种方式,使用流

    StreamWriter sw = new StreamWriter(fs,Encoding.Default);

    第二种方式,不使用流

    StreamWriter sw = new StreamWriter(path,true,Encoding.Default);

    将字符串写入文件
    sw.Write(richTextBox1.Text);

    关闭
    sw.Close();
    fs.Close();

    StreamRead类:

    取文件路径
    string path = saveFileDialog1.FileName;

    打开文件流
    FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write);

    第一种方式,使用流
    StreamReader sr = new StreamReader(fs,Encoding.Default);

    第二种方式,不适用流
    StreamReader sr = new StreamReader(path,Encoding.Default);

    读一行
    richTextBox1.Text = sr.ReadLine();

    读所有
    richTextBox1.Text = sr.ReadToEnd();

    关闭
    sr.Close();

     

    对话框:
    ColorDialog颜色选择控件

    colorDialog1.ShowDialog();

    button1.BackColor = colorDialog1.Color;

    FolderBrowserDialog文件夹选择控件

    folderBrowserDialog1.ShowDialog();

    label1.Text = folderBrowserDialog1.SelectedPath;

    FontDialog字体样式选择控件

    fontDialog1.ShowDialog();

    label1.Font = fontDialog1.Font;

    openFileDialog文件选择控件

    DialogResult dr = openFileDialog1.ShowDialog();

    if(dr == DialogResult.OK)
    {
    label1.Text = openFileDialog1.FileName;
    }

    限制打开的文件后缀Filter = 文本文件|*.txt|所有文件|*.*;
    一个显示一个后缀是一组,添加另一组需要继续使用'|'

    saveFileDialog保存路径选择控件

    saveFileDialog1.ShowDialog();

    label1.Text = saveFileDialog1.FileName;

  • 相关阅读:
    jquery直接操作元素的方式
    ie6下,给a添加事件,如果事件中有http请求,将会无效
    一个Tahoma字体bug引发的思考—关于样式bug的分析流程
    用弧度画圆
    【译】OWIN: Open Web Server Interface for .NET
    【译】Dependency Injection with Autofac
    Asp.net Identity身份与权限体系设计
    winform 数据(双向)绑定 快速更新实体
    泛型与非泛型的区别。
    使用XmlReader读Xml
  • 原文地址:https://www.cnblogs.com/zzzy0828/p/5849933.html
Copyright © 2011-2022 走看看