• 大小: 0.04M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-28
  • 语言: C#
  • 标签: string  文本  

资源简介


资源截图

代码片段和文件信息

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

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

        
        private static Boolean createutf_8file(string strText string strPath)
        {
            //声明一个Boolean变量返回文件是否新增成功
            Boolean blstate = false;
            if (!File.Exists(strPath)) //根据文件路径判断文件是否存在
            {
                //使用using语句创建实现了IDisposable接口的类,当using语句块结束后,
                //程序会自动调用IDisposable接口的Dispose方法来销毁创建的实例。
                using (StreamWriter sw = File.CreateText(strPath))  //创建用于写入 UTF-8 编码的文本
                {
                    sw.Write(strText); //向写入流中写入内容
                    blstate = true; //成功即返回true
                }
            }
            else //文件存在就删除文件
            {
                File.Delete(“D:\\增删改\\test1.txt“); //使用File 操作文件的静态类的Delete的方法删除文件
                using (StreamWriter sw = File.CreateText(strPath))  //创建用于写入 UTF-8 编码的文本
                {
                    sw.Write(strText); //向写入流中写入内容
                    blstate = true; //成功即返回true
                }
                return true;
            }
            return blstate;
        }

        private void button1_Click(object sender EventArgs e)
        {
            try
            {
                //OpenFileDialog ofdg = new OpenFileDialog();//打开文件对话框
                //if (ofdg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                //{
                //    string file = ofdg.FileName;
                //    textBox2.Text = System.IO.File.ReadAllText(file Encoding.Default);
                //}


                openFileDialog1.ShowDialog();  //打开选择文件对话框
                TextFileName.Text = openFileDialog1.FileName;  //获取打开的文件名
                FileStream fs = File.OpenRead(openFileDialog1.FileName); //打开现有文件以进行读取
                byte[] bs = new byte[1024]; //定义缓存
                if (fs.Read(bs 0 bs.Length) > 0)   //每次读取1024个字节 ,判断结果是否大于0
                {
                    textContext.Text = Encoding.Default.GetString(bs); //把字节数组所有字节转为一个字符串

                    //textContext.Text = Convert(byte). System.IO.File.ReadAllText(bs Encoding.Default);
                }
                fs.Close(); //关闭文件流
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// 
        /// 写入按钮点击事件(向文件写入内容)
        /// 

        /// 
        /// 
        private void btnWrite_Click(object sender EventArgs e)
        {

            if (TextFileName.Text != ““)
            {
                if (textContext.Text != ““)
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-09-27 09:24  FileReadWrite\.vs\
     目录           0  2020-09-27 09:24  FileReadWrite\.vs\FileReadWrite\
     目录           0  2020-09-27 09:24  FileReadWrite\.vs\FileReadWrite\v16\
     文件       55296  2020-09-28 10:41  FileReadWrite\.vs\FileReadWrite\v16\.suo
     文件        1145  2020-09-27 09:24  FileReadWrite\FileReadWrite.sln
     目录           0  2020-09-28 10:22  FileReadWrite\FileReadWrite\
     文件         187  2020-09-27 09:24  FileReadWrite\FileReadWrite\App.config
     目录           0  2020-09-27 17:45  FileReadWrite\FileReadWrite\bin\
     目录           0  2020-09-27 09:51  FileReadWrite\FileReadWrite\bin\Debug\
     文件       11264  2020-09-28 10:22  FileReadWrite\FileReadWrite\bin\Debug\FileReadWrite.exe
     文件         187  2020-09-27 09:24  FileReadWrite\FileReadWrite\bin\Debug\FileReadWrite.exe.config
     文件       36352  2020-09-28 10:22  FileReadWrite\FileReadWrite\bin\Debug\FileReadWrite.pdb
     目录           0  2020-09-27 17:45  FileReadWrite\FileReadWrite\bin\Release\
     文件        3774  2020-09-27 17:45  FileReadWrite\FileReadWrite\FileReadWrite.csproj
     文件        7121  2020-09-28 10:22  FileReadWrite\FileReadWrite\Form1.cs
     文件        5721  2020-09-27 20:12  FileReadWrite\FileReadWrite\Form1.Designer.cs
     文件        6020  2020-09-27 20:12  FileReadWrite\FileReadWrite\Form1.resx
     目录           0  2020-09-27 09:24  FileReadWrite\FileReadWrite\obj\
     目录           0  2020-09-28 10:21  FileReadWrite\FileReadWrite\obj\Debug\
     文件         210  2020-09-27 09:24  FileReadWrite\FileReadWrite\obj\Debug\.NETframeworkVersion=v4.8.AssemblyAttributes.cs
     文件        1423  2020-09-28 10:13  FileReadWrite\FileReadWrite\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7345  2020-09-27 17:45  FileReadWrite\FileReadWrite\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件          42  2020-09-27 19:35  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.csproj.CoreCompileInputs.cache
     文件         825  2020-09-27 16:45  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.csproj.FileListAbsolute.txt
     文件        1012  2020-09-27 20:12  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.csproj.GenerateResource.cache
     文件         424  2020-09-28 10:21  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.csprojAssemblyReference.cache
     文件       11264  2020-09-28 10:22  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.exe
     文件         180  2020-09-27 20:12  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.Form1.resources
     文件       36352  2020-09-28 10:22  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.pdb
     文件         180  2020-09-27 19:35  FileReadWrite\FileReadWrite\obj\Debug\FileReadWrite.Properties.Resources.resources
     目录           0  2020-09-27 09:24  FileReadWrite\FileReadWrite\obj\Debug\TempPE\
............此处省略7个文件信息

评论

共有 条评论