栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C#创建Windows窗体应用程序实例12【文件管理】

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#创建Windows窗体应用程序实例12【文件管理】

都实例11了?你还不会创建项目?不会操作步骤?不会写代码?请进入下方链接学习吧!

C#创建Windows窗体应用程序实例1

C#创建Windows窗体应用程序实例2

1. 知识点

打开文件并读出文本  方法1

        StreamReader  rd1=new StreamReader(@”c:a.txt”);  

        while((line=rd1.ReadLine())!=null)  { Console.WriteLine(line); }      

        rd1.Close();

打开文件并读出文本  方法2

        using(StreamReader  d1=new StreamReader(@”c:a.txt”)) {    

                string line;        

                while((line=rd1.ReadLine())!=null) { 

                        Console.WriteLine(line); 

                        } 

        } 

打开文件并写入文本   方法1

        StreamWriter  sw=new StreamWriter(@”c:a.txt”);

        sw.WriteLine(“Suzhou”);

        sw.WriteLine(“125.3”); sw.Close();

打开文件并写入文本   方法2

        using(StreamWriter  sw=new StreamWriter(@”c:a.txt”)) {        

                sw.WriteLine(“Suzhou”);        

                sw.WriteLine(“125.3”);

        }

创建FileStream对象(打开文件)

        方法1 FileStream  fs=new FileStream(@”c:a.txt”,FileMode.Create);

        方法2 FileStream  fs=File.Create(@”c:a.txt”);

文件定位

        文件开始 fs.Seek(0,SeekOrigin.Begin);

        当前位置 fs.Seek(0,SeekOrigin.Current);

        文件结尾 fs.Seek(0,SeekOrigin.End);

写入/读取数据

        byte[] info=new UTF8Encoding(true).GetBytes(“A”);

        fs.Write(info, 0, info.Length); //写入字节数据    

        byte[] b=new byte[1024];

        fs.Read(b, 0, b.Length); //读取字节数据

关闭流

        fs.Close();  

        或采用 using 语句

2. 样式设计

在工具中选择 以下 控件

 

 

 

 

3. 代码实现 

using System.Windows.Forms;
using System.IO;

namespace 显示和保存文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        //”显示文件”的Click事件的代码
        private void button1_Click(object sender, EventArgs e)
        {
            string file = textBox1.Text;
            if (!File.Exists(@file))
                MessageBox.Show("文件" + @file + "不存在");
            else
            {
                int a;
                FileStream fs = new FileStream(@file, FileMode.Open, FileAccess.Read);
                richTextBox1.Clear();
                a = fs.ReadByte();
                while (a != -1)       //是否读到文件末尾
                {
                    richTextBox1.Text += ((char)a).ToString();
                    a = fs.ReadByte();
                }
                fs.Close();
            }
        }
        //”保存文件”的Click事件的代码
        private void button2_Click(object sender, EventArgs e)
        {
            byte a;
            string file = textBox1.Text;
            FileStream fs = new FileStream(@file, FileMode.OpenOrCreate, FileAccess.Write);
            for (int i = 0; i < richTextBox1.Text.Length; i++)
            {
                a = (byte)richTextBox1.Text[i];
                fs.WriteByte(a);
            }
            fs.Flush();   //将写缓冲区的数据写入内存
            fs.Close();
        }
    }
}
4. 执行效果  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号