基本信息
源码名称:c#文件读写示例代码下载
源码大小:1.62M
文件格式:.zip
开发语言:C#
更新时间:2015-04-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text;
            string content = richTextBox1.Text;
            if (string.IsNullOrEmpty(path) == true)
            {
                MessageBox.Show("文件件路径不能为空");
                return;
            }
            try
            {
                //创建文件流
                FileStream myfs = new FileStream(path, FileMode.Create);
                //创建写入器
                StreamWriter mysw = new StreamWriter(myfs);
                //执行写入器,将录入的内容写入文件
                mysw.Write(content);
                //关闭写入器
                mysw.Close();
                //关闭文件流
                myfs.Close();
                MessageBox.Show("写入成功");

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message .ToString ());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text;
            string content;
            if (string.IsNullOrEmpty(path) == true)
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
                StreamReader mysr = new StreamReader(myfs);
                content = mysr.ReadToEnd();
                richTextBox1.Text = content;
                mysr.Close();
                myfs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message .ToString ());
            }

        }
    }
}