C#文件操作

判断存在

bool System.IO.File.Exists(@"E:\project\123.txt");//判断文件是否存在
Directory.Exists(appdata_path);//判断文件夹是否存在
Directory.CreateDirectory(appdata_path);//产生文件路径

读取文件

当文件不是很大的时候File类方法读取

string str = File.ReadAllText(@"c:\temp\ascii.txt");//读取全部文件内容
string str2 = File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII);//用指定编码方式 读取全部内容
string[] strs = File.ReadAllLines(@"c:\temp\ascii.txt"); //读取全部文件内容 返回字符串数组 每一行一个元素(不包括换行)
string[] strs2 = File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);//读取全部文件内容 用指定编码方式

StreamReader类读取

命名空间

using System.IO;

初始化StreamReader类

//初始化StreamReader类
//方法一(最常用)
StreamReader sr = new StreamReader(@"c:\temp\utf-8.txt", Encoding.UTF8);//创建streamreader类,可以不用编码
//方法二
FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.Open, FileAccess.Read, FileShare.None); 
StreamReader sr = new StreamReader(fs, Encoding.UTF8);//创建streamreader类,可以不用编码
//方法三
FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); // OpenText 创建一个UTF-8 编码的StreamReader对象 
StreamReader sr = myFile.OpenText();
//方法四
StreamReader sr = File.OpenText(@"C:\temp\utf-8.txt");

读取

// 初始化完成过后
// 读一行 
string nextLine = sr.ReadLine();
// 读一个字符
int nextChar = sr.Read();// 读一个字符 注意 返回类型是int
// 读100个字符 
int nChars = 100;
char[] charArray = new char[nChars];
int nCharsRead = sr.Read(charArray, 0, nChars);
// 全部读完 
string restOfStream = sr.ReadToEnd();

关闭流

sr.Close();


逐行流读取范例

StreamReader sr = File.OpenText(@"C:\temp\ascii.txt");
string nextLine; 
while ((nextLine = sr.ReadLine()) != null) 
{ 
    Console.WriteLine(nextLine); 
}
sr.Close();

写入文件

当写入的内容不是很多用File类的方法

string str = "Good Morning!";
File.WriteAllText(@"c:\temp\test\ascii-2.txt", str, Encoding.ASCII);//可以不指定编码
//写入字符串数组
string[] strs = { "Good Morning!", "Good Afternoon!" };
File.WriteAllLines(@"c:\temp\ascii-2.txt", strs, Encoding.ASCII);//可以不指定编码

使用流(Stream)的方式写入

初始化流

//初始化流 方式1
//true 是 append text, false 为覆盖原文件 ,可省略,编码也可省略
StreamWriter sw2 = new StreamWriter(@"c:\temp\utf-8.txt", true, Encoding.UTF8);
//初始化流 方式2
FileMode.CreateNew: 如果文件不存在,创建文件;如果文件已经存在,抛出异常 
FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.CreateNew, FileAccess.Write, FileShare.Read);
StreamWriter sw4 = new StreamWriter(fs, Encoding.UTF8);// 可省略编码 UTF-8 为默认编码 
//初始化流 方式3
//如果文件不存在,创建文件; 如果存在,覆盖文件 
FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt");
StreamWriter sw5 = myFile.CreateText();

写入        

//写一个字符            
sw.Write('a');
//写入字符串
sw.Write("123123123");
//写入一行
sw.WriteLine("123312414");
//写一个字符数组 
char[] charArray = new char[100];
initialize these characters 
sw.Write(charArray);
//写一个字符数组的一部分 
sw.Write(charArray, 10, 15);

关闭流       

//sw.Close();

多行写入范例

FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt");
StreamWriter sw = myFile.CreateText();
string[] strs = { "早上好", "下午好" };            
foreach (var s in strs) 
{ 
    sw.WriteLine(s); 
}
sw.Close();

文件夹操作

若不存在文件夹路径则创建

string listpath = @"D:\test\test\";//文件夹路径
if (Directory.Exists(listpath)==false){Directory.CreateDirectory(listpath);}//如果不存在就创建文件夹

判断文件是否存在

添加引用using System.IO;

if(File.Exists(@"文件路径"))
{//存在文件

}
else
{//不存在文件

}

获得当前路径

//获取模块的完整路径。
//D:\work\bin\Release\XmlAndXsd.vshost.exe
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
//D:\work\Release
string path2 = System.Environment.CurrentDirectory;

//获取应用程序的当前工作目录
//D:\work\Release
string path3 = System.IO.Directory.GetCurrentDirectory();

//获取程序的基目录
//D:\work\Release\
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;

//获取和设置包括该应用程序的目录的名称
//D:\work\Release
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

//获取启动了应用程序的可执行文件的路径
//D:\work\Release
string path6 = System.Windows.Forms.Application.StartupPath;

//获取启动了应用程序的可执行文件的路径及文件名
//D:\work\XmlAndXsd.EXE
string path7 = System.Windows.Forms.Application.ExecutablePath;

我的文档路径

string str1 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//C:\Users\Administrator\Documents

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值