C#之文件的读写(一)

摘要:
“);}catch{MessageBox.Show(”磁盘操作错误,原因:“+Convert.ToString(mm),”prompt!“);}}//////移动目录////////privatevoidbutton3_单击{try{directory.Move;MessageBox.Show(“文件夹移动成功”,“提示!”);否则{directory_annotherpath=directory_annotherpath+textBox1.Text.Trim()+“.txt”;StreamWritersw=File.CreateText;richTextBox1.Enabled=true;MessageBox.Show(“文件创建成功。

整个系统的界面如下:

C#之文件的读写(一)第1张

先上代码:

C#之文件的读写(一)第2张C#之文件的读写(一)第3张
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;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static string directory_path = @"E:studycodeC#winform2017-12-9WindowsFormsApp11";
        private static string directory_otherpath = @"E:studycodeC#winform2017-12-9";
        private static string directory_anotherpath = "E:\study\code\C#\winform\2017-12-9\WindowsFormsApp1\";
        //private static string directory_vv = "E:\study\code\C#\winform\2017-12-9\WindowsFormsApp1\12.txt";
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.CreateDirectory(directory_path);
                button1.Enabled = false;
                button2.Enabled = true;
                button3.Enabled = true;
                button1.Enabled = true;
                MessageBox.Show("文件夹成功建立","提示!");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }
        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(directory_path);
                button2.Enabled = false;
                button1.Enabled = true;
                button3.Enabled = false;
                button4.Enabled = false;
                MessageBox.Show("文件夹删除成功","提示!");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");               
            }
        }
        /// <summary>
        /// 移动目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Move(directory_path, directory_otherpath);
                MessageBox.Show("文件夹移动成功", "提示!");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
                
            }
        }
        /// <summary>
        /// 目录创建时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)),"提示!");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }

        /// <summary>
        /// 创建文本文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

            try
            {
                if (textBox1.Text.Length == 0)
                    MessageBox.Show("文件名禁止为空", "提示!");
                else
                {
                    directory_anotherpath = directory_anotherpath + textBox1.Text.Trim() + ".txt";
                    StreamWriter sw = File.CreateText(directory_anotherpath);
                    richTextBox1.Enabled = true;
                    MessageBox.Show("文件创建成功。", "提示!");
                    sw.Close();
                }
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }
        /// <summary>
        /// 打开文本文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Title = "打开文本文件";
                open.FileName = "";
                open.AddExtension = true;
                open.CheckFileExists = true;
                open.CheckPathExists = true;
                open.Filter = "文本文件(*.txt) | *.txt";
                open.ValidateNames = true;
                if (open.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(open.FileName,System.Text.Encoding.Default);
                    this.richTextBox1.Text = sr.ReadToEnd();
                    MessageBox.Show("文件打开成功","提示!");
                    richTextBox1.Enabled = true;
                    sr.Close();
                }
                
                
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }

        /// <summary>
        /// 保存编辑文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream textfile = File.Open(directory_anotherpath, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(textfile, Encoding.GetEncoding("GB2312"));
                sw.WriteLine(richTextBox1.Text.ToString());
                MessageBox.Show("文件写成功","提示!");
                sw.Close();
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }
    }
}
代码在这哦

分析:

分析代码进行窗口调试调整的时候,用try catch finally 组合会使得整个程序不会出现致命性异常。

 

创建目录:

 C#之文件的读写(一)第4张

使用directory类的创建目录方法的功能。

directory_path 是自己设定的文件路径,注意有@和没@得区别。

删除目录和移动目录应用的同样是Directory类中的方法。

目录创建时间:

MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)),"提示!");

Format是String类中的一个格式化数据的一个方法。

C#之文件的读写(一)第5张

创建文本文件:

C#之文件的读写(一)第6张

CreateText(string FilePath) 方法是创建或打开一个文件,用于写入UTF-8 编码的文件

C#之文件的读写(一)第7张

File.CreateText 返回的一个StreamWriter对象。使用完要关闭。

 sw.Close();

打开文本文件:

OpenFileDialog 是创建一个打开的对话框

C#之文件的读写(一)第8张

其中要设置对话框的属性:

C#之文件的读写(一)第9张

C#之文件的读写(一)第10张

基本属性设置。

    StreamReader sr = new StreamReader(open.FileName,System.Text.Encoding.Default);
    this.richTextBox1.Text = sr.ReadToEnd();

C#之文件的读写(一)第11张

fileName是选择后的文件打开路径,Encoding.Default是获取操作系统当前ANSI代码页的编码。(ANSI代码是一种字符代码)

C#之文件的读写(一)第12张

打开读取成功后要记得关闭StreamReader的对象。

保存编辑文件:

  FileStream textfile = File.Open(directory_anotherpath,  FileMode.OpenOrCreate, FileAccess.Write);

设置读写模式和访问权限的流。

定义读的流对象,并将FileStream对象写入StreamReader对象中,并设置格式。

保存成功后要关闭。

免责声明:文章转载自《C#之文件的读写(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue 移动端车牌键盘centos8平台使用nethogs基于进程监控网络流量下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

主流服务器虚拟化技术简单使用——Xen(二)

管理多台Xen主机可以使用GUI工具virt-manager和xm、xl等命令行工具。   Tips:hypervisor一定要选到Xen web管理工具 Xen也有一个简易web管理工具叫xenwebmanager,相当于KVM的WebVirtMgr。可惜的是这个项目的作者明确说明该项目已经死掉了(可能也是受到docker的影响),而且网上也没有找到什...

uniapp 实现动态切换全局主题色

需求:实现开发的应用中切换主题色 如果只是需要一个主题色没有切换的需要 完全可以使用uniapp里面uni.scss文件文档 思路:预先在一个公共css中定义你需要的主题颜色,这里只是示例定义了两种颜色 参考文档 从中获得思路可以通过动态设置data-xx从而配合css属性选择器来动态改变主题色 本来是想通过mixin直接混入一个变量来达到全局控住主题...

快思软件,工具资料及学习网

1、快思聪官网手册   https://help.crestron.com/simpl_sharp/   https://help.crestron.com/SimplSharp/html/N_Crestron_SimplSharpPro.htm  (pro主机)   https://help.crestron.com/   https://help.cr...

egg内置对象

http://eggjs.org/zh-cn/basics/objects.html 目录 egg内置对象 1.1 Application 1.2 Context 1.3 Request and Response 1.4 Helper(扩展) 1.5 Config 1.6 Logger egg内置对象 框架内置基础对象:从 Koa 继承而来的...

windows用navict for mongo 把mongo 数据库中的数据全部导入另一个数据库

mongodb 在导出数据的可以使用命令,也可以使用navicat 这种可视化软件,我就选择的使用navicat 转移数据。 具体的业务场景是,线上的数据,导入到测试环境一份,供前端调试。 使用navicat for mongo 要安装navict 还要,安装mongodb-database-tools-windows-x86_64-100.5.1.m...

获取谷歌浏览器缓存视频方法

一、首先找到缓存文件位置C:UsersJiaPengAppDataLocalGoogleChromeUser DataDefaultCache 如果不好找,可以先打开IE浏览器,Internet选项-》常规下点击设置-》Internet临时文件下点击查看文件 得到IE缓存文件的位置C:UsersJiaPengAppDataLocalMicrosoftWi...