怎样在C#中对文件操作(在线等)

Fi 2003-08-19 04:02:14
打开一个文件,读取里面的内容,应该怎么做?
...全文
44 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chagel 2003-08-19
  • 打赏
  • 举报
回复
Using FileStreamReader and FileStreamWriter or StreamReader and StreamWriter class.
雪狼1234567 2003-08-19
  • 打赏
  • 举报
回复
读写:
using System;
using System.IO;
public class TextFromFile {
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {
if (!File.Exists(FILE_NAME)) {
return;
}
StreamReader sr = File.OpenText(FILE_NAME);
String input;
while ((input=sr.ReadLine())!=null) {
//input................
}

sr.Close();
}
}


using System;
using System.IO;
public class TextToFile {
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {
if (File.Exists(FILE_NAME)) {
return;
}
StreamWriter sr = File.CreateText(FILE_NAME);
sr.WriteLine ("字符串");
sr.Close();
}
}
qiujinwen 2003-08-19
  • 打赏
  • 举报
回复
try
{
StreamReader sr=new StreamReader(filepath);
sr.BaseStream.Seek(0,SeekOrigin.Begin);
while (sr.Peek()>-1)
{
string tmpstr=sr.ReadLine();
string[] strarr=tmpstr.Split('=');
switch (strarr[0].Trim())
{
case "RootDir":
你的操作
break;

}
}
sr.Close();
}
catch(Exception e)
{
//sr.Close();
throw e;
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧