• 大小: 43KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C#
  • 标签: C#  TXT  文件  读写  

资源简介

C#:TXT文件读写(VS2010窗体+代码)

资源截图

代码片段和文件信息

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;
using System.IO;

namespace txt读写
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //选择并读取TXT文件
        private void btn_Path_Click(object sender EventArgs e)
        {
            OpenFileDialog xjOpenFileDialog = new OpenFileDialog();
            xjOpenFileDialog.Filter = “文本文件|*.txt“;
            if (xjOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                string xjFilePath=xjOpenFileDialog.FileName;
                this.txt_Path.Text = xjFilePath;//显示文件路径

                StreamReader sr = new StreamReader(xjFilePath Encoding.Default);
                this.txt_Content.Text = sr.ReadToEnd();//显示内容
            }
        }

        //写入TXT文件
        private void btn_In_Click(object sender EventArgs e)
        {
            SaveFileDialog xjSaveFileDialog = new SaveFileDialog();
            xjSaveFileDialog.Filter = “文本文件|*.txt“;
            if (xjSaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string xjFilePath = xjSaveFileDialog.FileName;
                this.txt_Path2.Text = xjFilePath;//显示文件路径

                if (!File.Exists(xjFilePath))
                {
                    FileStream xjFileStream = new FileStream(xjFilePath FileMode.Create FileAccess.Write);
                    StreamWriter xjStreamWriter = new StreamWriter(xjFileStream Encoding.Default);
                    xjStreamWriter.WriteLine(this.txt_Content.Text);//要写入的
                    xjStreamWriter.Close();
                    xjFileStream.Close();
                    MessageBox.Show(“保存成功!“);
                }
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10240  2017-11-03 15:35  txt读写\txt读写\bin\Debug\txt读写.exe

     文件      24064  2017-11-03 15:35  txt读写\txt读写\bin\Debug\txt读写.pdb

     文件      11600  2017-11-03 16:14  txt读写\txt读写\bin\Debug\txt读写.vshost.exe

     文件        490  2010-03-17 22:39  txt读写\txt读写\bin\Debug\txt读写.vshost.exe.manifest

     文件       2026  2017-11-03 15:31  txt读写\txt读写\MainForm.cs

     文件       5840  2017-11-03 15:00  txt读写\txt读写\MainForm.Designer.cs

     文件       5817  2017-11-03 15:00  txt读写\txt读写\MainForm.resx

     文件       4501  2017-11-03 14:44  txt读写\txt读写\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6353  2017-11-03 16:14  txt读写\txt读写\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        170  2017-11-03 15:00  txt读写\txt读写\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        430  2017-11-03 15:00  txt读写\txt读写\obj\x86\Debug\GenerateResource.write.1.tlog

     文件       8031  2017-11-03 14:45  txt读写\txt读写\obj\x86\Debug\ResolveAssemblyReference.cache

     文件        843  2017-11-03 16:14  txt读写\txt读写\obj\x86\Debug\txt读写.csproj.FileListAbsolute.txt

     文件      10240  2017-11-03 15:35  txt读写\txt读写\obj\x86\Debug\txt读写.exe

     文件        180  2017-11-03 15:00  txt读写\txt读写\obj\x86\Debug\txt读写.MainForm.resources

     文件      24064  2017-11-03 15:35  txt读写\txt读写\obj\x86\Debug\txt读写.pdb

     文件        180  2017-11-03 08:59  txt读写\txt读写\obj\x86\Debug\txt读写.Properties.Resources.resources

     文件        493  2017-11-03 08:33  txt读写\txt读写\Program.cs

     文件       1368  2017-11-03 08:32  txt读写\txt读写\Properties\AssemblyInfo.cs

     文件       2866  2017-11-03 08:32  txt读写\txt读写\Properties\Resources.Designer.cs

     文件       5612  2017-11-03 08:32  txt读写\txt读写\Properties\Resources.resx

     文件       1092  2017-11-03 08:32  txt读写\txt读写\Properties\Settings.Designer.cs

     文件        249  2017-11-03 08:32  txt读写\txt读写\Properties\Settings.settings

     文件       3692  2017-11-03 08:53  txt读写\txt读写\txt读写.csproj

     文件        869  2017-11-03 08:33  txt读写\txt读写.sln

    ..A..H.     18432  2017-11-03 16:18  txt读写\txt读写.suo

     目录          0  2017-11-03 08:32  txt读写\txt读写\obj\x86\Debug\TempPE

     目录          0  2017-11-03 15:35  txt读写\txt读写\obj\x86\Debug

     目录          0  2017-11-03 14:44  txt读写\txt读写\bin\Debug

     目录          0  2017-11-03 08:33  txt读写\txt读写\bin\Release

............此处省略9个文件信息

评论

共有 条评论