借鉴网上, 重新写了txt操作类
控制台程序
1. 目录结构

2. TxtHelper.cs
有两个成员函数, 读和写
如果文件路径不存在, 会新建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace TxtHelper
{
class TxtHelper
{
public TxtHelper()
{
}
public void Write(string path, string content)
{
try
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(content);
sr.Close();
fs.Close();
}
catch (Exception)
{
throw;
}
}
public string ReadFirstLine(string path)
{
if

这是一个关于C#编程的博客,作者重新编写了一个用于读写txt文件的辅助类 TxtHelper。该类包含读取和写入两个功能,如果指定的文件路径不存在,类会自动创建新文件。博客通过一个控制台程序展示了类的使用方法,并给出了执行结果。
最低0.47元/天 解锁文章
366

被折叠的 条评论
为什么被折叠?



