资源简介
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
- 上一篇:C#视频采集源码(视频技术)
- 下一篇:C#多媒体定时器 工具
相关资源
- C#多媒体定时器 工具
- C#视频采集源码(视频技术)
- 可调透明度的txt阅读器[C#]
- C# 通过存储过程将Excel文档导入SQL S
- c#版五子棋人机对弈模式
- C#中国姓名自动生成代码
- C#媒体播放器,支持多种格式
- C#非接触IC卡M1卡读写调试源代码(适
- C#简单与数据库连接的登录界面
- C#设置IP地址启用禁用适配器
- C# System Hooks
- WPF 自定义窗口风格,带TOOLBAR
- 自来水收费管理系统c# 源代码
- WPFC#.net 跨线程更新UI 线程测试 解决界
- c#写的简单嗅探器(Socket)
- 在c#中ole方式加入excel
- 时间序列预测
- C# WinFrom 挂机锁屏功能实现()
-
C#的Winform多语言实现xm
l文件升级版 - C#的Winform多语言实现resx文件
- SQL Server数据库操作类(C#)
- vs2008 c#.net web二维码生成系统
- (c#)使用SHGetFileInfo获取文件或文件
- C#+使用word模板+批量打印例子192182
- C#导出数据到EXCEL方法谈附和超级无敌
- C#+AE根据点坐标生成shp文件
- seafile私有云的api的C#客户端实现
- c#窗体嵌入应用程序
- 游戏账户管理系统
- 利用C#读取DEM数据,求坡度
评论
共有 条评论