C# 对记事本的读写

1 篇文章 0 订阅
1 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Anshield_AutoFutures.BaseClase
{
   public class TXT_Clase
    {
     
       /// <summary>
        /// 文本内容写入
       /// </summary>
       /// <param name="infoContent">数据流</param>
       /// <param name="FilePath">路径</param>
       /// <param name="fileName">文件名</param>
        public static void FileWrite(string infoContent, string FilePath,string fileName)
        {
            try
            {
                string filePath=(FilePath + fileName);
                //路径不存在则创建
                if (Directory.Exists(FilePath) == false)
                {
                    Directory.CreateDirectory(FilePath);
                }

                FileInfo file = new FileInfo(FilePath + fileName);//文件地址
                File.AppendAllText(filePath, infoContent+ Environment.NewLine, Encoding.GetEncoding("gb2312"));
               /*
                //方法二
                if (file.Exists==false)//不存在文件
                {
                    
                   
                    //创建日志文件
                    file.Create();
                    修改文件属性为可写入和取消隐藏
                    //if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    //{
                    //    file.Attributes = file.Attributes & ~FileAttributes.ReadOnly; // 法一
                    //}
                    using (StreamWriter sw = file.CreateText())
                    {
                       
                        sw.WriteLine(infoContent);//写入一行,写完后会自动换行
                        sw.Flush();//将缓冲区内在写入到基础流中
                        sw.Close();
                    }
                    
                }
                else
                {                   
                    修改文件属性为可写入和取消隐藏
                    //if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    //{
                    //    file.Attributes = file.Attributes & ~FileAttributes.ReadOnly; // 法一
                    //}
                    //存在 ,将内容追加到记事本                 
                    using (StreamWriter sw = file.AppendText())
                    {
                        sw.WriteLine(infoContent);
                        sw.Flush();//将缓冲区内在写入到基础流中
                        sw.Close();
                    }                     
                }
                * */
              
            }
            catch (FileNotFoundException fileCe)
            {
                throw fileCe;
            }
            catch (Exception ce)
            {
                throw ce;
            }
        }
        /// <summary>
        /// 读取记事本
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string  FileRead(string FilePath)
        {
            string inforContent ="";
            FileInfo file = new FileInfo(FilePath);
            if (file.Exists==true)
            {              
                try
                {
                    FileStream stream = new FileStream(FilePath, FileMode.Open);
                    StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"));//Encoding.GetEncoding("gb2312"),Encoding.UTF8
                    //reader.ReadLine(); //一次性读取一行
                    inforContent = reader.ReadToEnd();  //一次性读取全部数据
                    reader.Close();
                    stream.Close();
                }
                catch (Exception ce)
                {
                    throw ce;
                }
            }
            return inforContent;
        }
        /// <summary>
        /// 得到一个文件下的所有目录
        /// </summary>
        /// <param name="filePath"></param>
        private void GetAllFiles(string filePath)
        {
            try
            {
                DirectoryInfo dInfo = new DirectoryInfo(filePath);
                //遍历该文件夹
             
            }
            catch { }
        }
        /// <summary>
        /// 得到文件列表
        /// </summary>
        /// <param name="dInfo">文件路径</param>
        public static FileInfo[] GetFiles(string filePath)
        {
              DirectoryInfo dInfo = new DirectoryInfo(filePath);
                FileInfo[] files =null ;
                if (dInfo.Exists)
                {
                    files = dInfo.GetFiles();
                    /*
                    //遍历文件夹中的文件
                    foreach (FileInfo file in dInfo.GetFiles())
                    {
                        //file.Name.Trim()
                  
                    }*/
                }             
                return files;
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bingo_BIG

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值