当前位置 : 主页 > 编程语言 > c语言 >

(2010-08-11) C#.NET System.IO

来源:互联网 收集:自由互联 发布时间:2021-06-25
摘要:(2010-08-06) C#.NET System.IO 范列1.读取图档后存档 程序 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Wind

摘要:(2010-08-06) C#.NET System.IO


范列1.读取图档后存档

程序 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winmod05
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //呈现对话盒
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //文件名称
                //MessageBox.Show(this.openFileDialog1.FileName);
                //1.建立串流
                System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName, 
                    System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //准备缓冲区 Byte数组 
                Byte[] buff = new Byte[fs1.Length];
                //开始读取 读到缓冲区去
                fs1.Read(buff, 0, (Int32)fs1.Length);
                //建立Gap(内存串流)
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ms.Write(buff, 0, (Int32)fs1.Length);
                //建构Image对象
                System.Drawing.Image img = new System.Drawing.Bitmap(ms);
                //设定PictureBox
                this.pictureBox1.Image = img;
                //写出图片到另一个地方 
                //建立串流 
                String newFile=System.IO.Path.GetFileNameWithoutExtension(this.openFileDialog1.FileName)+"_new.jpg";
                //MessageBox.Show(newFile);
                System.IO.Stream fout = 
                    new System.IO.FileStream(@"c:images" + newFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //写出去
                fout.Write(buff, 0, (Int32)fs1.Length);
                //清缓冲区
                fout.Flush();
                fout.Close();
                //关闭串流
                fs1.Close();
            }
        }
    }
}

范列2.笔记本 读档与写档

程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winmod05
{
    public partial class NotePad : Form
    {
        public NotePad()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //启动对话盒
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //处理
                //1.建立串流 
                System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //2.接上读取器
                System.IO.TextReader reader = new System.IO.StreamReader(fs1, System.Text.Encoding.UTF8);
                String text = reader.ReadToEnd();
                this.textBox1.Text = text;
                fs1.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //保存
                //1.建立串流
                System.IO.Stream fs1 = new System.IO.FileStream(this.saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //2.建立Writer
                System.IO.StreamWriter writer = new System.IO.StreamWriter(fs1, System.Text.Encoding.UTF8);
                writer.Write(this.textBox1.Text);
                writer.Flush();
                writer.Close();
                fs1.Close();               

            }
        }
    }
}

补充:

原文:大专栏  (2010-08-11) C#.NET System.IO

网友评论