• 大小: 23KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: C#
  • 标签: C#  文件  读取  写入  操作  

资源简介

C#文件的读取与写入操作及其读取数据处理(注释详尽,相对路径,直接运行,测试成功!)

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO; // 文件操作的命名空间

namespace ReadWriteFileProcess
{
    class Program
    {
        // 主函数
        static void Main(string[] args)
        {
            // 读取和写入文件的目录
            string strReadFilePath=@“..\..\data\ReadLog.txt“;
            string strWriteFilePath = @“..\..\data\WriteLog.txt“;

            // 读取读文件,并打印在控制台下
            string strReadFile = GetReadFile(strReadFilePath);
            Console.WriteLine(strReadFile);

            // 判断读文件是否操作成功,并正确写入到了写文件
            bool WriteOk = IsWriteReadFile(strWriteFilePath strReadFile);
            Console.WriteLine(WriteOk);

            // 读取写文件,并打印在控制台下
            string strReadWriteFile = GetReadFile(strWriteFilePath);
            Console.WriteLine(strReadWriteFile);

            Console.ReadKey();
        }

        // 获取文件内容
        public static string GetReadFile(string strReadFilePath)
        {
            string strReadFile = string.Empty;
            StreamReader srReadFile=new StreamReader(strReadFilePath);

            if (!srReadFile.EndOfStream)
                strReadFile=srReadFile.ReadLine();
            
            while (!srReadFile.EndOfStream)
            {
                string strReadLine = srReadFile.ReadLine();
                strReadFile += “\n“+strReadLine;
            }

            srReadFile.Close();

            return strReadFile;
        }

        // 写入文件内容
        public static bool IsWriteReadFile(string strWriteFilePath string strWriteFile)
        {
            StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

            string[] WriteLines = strWriteFile.Split(new char[]{ ‘\n‘ });
            if (WriteLines.Length <= 0)
                return false;

            int i = 0;
            string strLine = string.Empty;
            int iPos = 0;
            for (i = 0; i < WriteLines.Length - 1; i++)
            {
                strLine = WriteLines[i];
                iPos = strLine.IndexOf(“ - Socket: “);
                strLine = (i + 1) + strLine.Substring(iPos);
                swWriteFile.WriteLine(strLine);
            }
            strLine = WriteLines[i];
            iPos = strLine.IndexOf(“ - Socket: “);
            strLine = (i + 1) + strLine.Substring(iPos);
            swWriteFile.Write(strLine);

            swWriteFile.Close();

            return true;
        }
    }
}

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

     文件       5632  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\bin\Debug\ReadWriteFileProcess.exe

     文件      13824  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\bin\Debug\ReadWriteFileProcess.pdb

     文件      14328  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\bin\Debug\ReadWriteFileProcess.vshost.exe

     文件       1641  2010-01-13 20:38  ReadWriteFileProcess\ReadWriteFileProcess\data\ReadLog.txt

     文件       1579  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\data\WriteLog.txt

     文件        466  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\obj\Debug\ReadWriteFileProcess.csproj.FileListAbsolute.txt

     文件       5632  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\obj\Debug\ReadWriteFileProcess.exe

     文件      13824  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\obj\Debug\ReadWriteFileProcess.pdb

     文件        100  2010-01-13 20:20  ReadWriteFileProcess\ReadWriteFileProcess\obj\Release\ReadWriteFileProcess.csproj.FileListAbsolute.txt

     文件       2688  2010-01-13 21:14  ReadWriteFileProcess\ReadWriteFileProcess\Program.cs

     文件       1452  2010-01-13 19:44  ReadWriteFileProcess\ReadWriteFileProcess\Properties\AssemblyInfo.cs

     文件       2513  2010-01-13 19:44  ReadWriteFileProcess\ReadWriteFileProcess\ReadWriteFileProcess.csproj

     文件        950  2010-01-13 19:44  ReadWriteFileProcess\ReadWriteFileProcess.sln

    ..A..H.     14848  2010-01-13 21:22  ReadWriteFileProcess\ReadWriteFileProcess.suo

     目录          0  2010-01-13 19:44  ReadWriteFileProcess\ReadWriteFileProcess\obj\Debug\TempPE

     目录          0  2010-01-13 20:22  ReadWriteFileProcess\ReadWriteFileProcess\bin\Debug

     目录          0  2010-01-13 20:20  ReadWriteFileProcess\ReadWriteFileProcess\bin\Release

     目录          0  2010-01-13 21:11  ReadWriteFileProcess\ReadWriteFileProcess\obj\Debug

     目录          0  2010-01-13 20:20  ReadWriteFileProcess\ReadWriteFileProcess\obj\Release

     目录          0  2010-01-13 20:20  ReadWriteFileProcess\ReadWriteFileProcess\bin

     目录          0  2010-01-13 21:06  ReadWriteFileProcess\ReadWriteFileProcess\data

     目录          0  2010-01-13 20:20  ReadWriteFileProcess\ReadWriteFileProcess\obj

     目录          0  2010-01-13 19:44  ReadWriteFileProcess\ReadWriteFileProcess\Properties

     目录          0  2010-01-13 21:14  ReadWriteFileProcess\ReadWriteFileProcess

     目录          0  2010-01-13 19:44  ReadWriteFileProcess

----------- ---------  ---------- -----  ----

                79477                    25


评论

共有 条评论