C# 目录、文件操作

一、文件操作

向文件中追加文本 File.AppendText FileInfo.AppendText
重命名或移动文件 File.Move FileInfo.MoveTo
删除文件 File.Delete FileInfo.Delete
复制文件 File.Copy FileInfo.CopyTo
获取文件大小 FileInfo.Length
获取文件属性 File.GetAttributes
设置文件属性 File.SetAttributes
确定文件是否存在 File.Exists
检索文件扩展名 Path.GetExtension
检索文件的完全限定路径 Path.GetFullPath
检索路径中的文件名和扩展名 Path.GetFileName
更改文件扩展名 Path.ChangeExtension

Stream//对字节的读写操作(包含对异步操作的支持)

BinaryReader和BinaryWriter//从字符串或原始数据到各种流之间的读写操作 

FileStream//文件流操作 

StringReader和StringWriter//在字符串中读写字符 

StreamReader和StreamWriter//在流中读写字符 

BufferedStream//为诸如网络流的其它流添加缓冲的一种流类型

MemoryStream//无缓冲的流 

NetworkStream//互联网络上的流 

//打开文件夹
System.Diagnostics.Process.Start(FilePath);
//打开文件夹中某个文件
System.Diagnostics.Process.Start(FilePath+"/"+FileName);
//打开文件夹并选中单个文件
System.Diagnostics.Process.Start("Explorer""/select,"+ FilePath+"\"+FileName);
//
System.Diagnostics.Process.Start("Explorer.exe""/select,"+ FilePath+"\"+FileName);
//用IE打开文件
System.Diagnostics.Process.Start("Explorer",FilePath+"\"+FileName);
System.Diagnostics.Process.Start("Explorer.exe",FilePath+"\"+FileName);
//注:(explorer,explorer.exe,select,不区分大小写,"/selecet,"其中"/,"都不能少,FilePath为文件路径不包含文件名)
//打开文件夹并选中网页

System.Diagnostics.Process.Start("Explorer.exe", ”http://www.sunwale.com/“); 

二、目录操作

string[] drives = Directory.GetLogicalDrives();//本地驱动器名称
string path = Directory.GetCurrentDirectory();//获取应用程序的当前工作目录
string[] files = Directory.GetFiles(@"D:\Projects");//获取指定目录中的文件名
string[] directories= Directory.GetDirectories(@"D:\");//获取指定目录中的目录名
DirectoryInfo di = new DirectoryInfo(@"D:\MyDir1");//构造函数创建目录
di.Create();
DirectoryInfo di1 = Directory.CreateDirectory(@"D:\MyDir2");//创建对象并创建目录
DirectoryInfo di2 = di.CreateSubdirectory("SubDir");//以相对路径创建子目录

string[] str=Directory.GetFileSystemEntries(@"D:\");//获取指定目录中的目录及文件名

三、编码转换 

Encoding e1 = Encoding.Default;//取得本页默认代码 
Byte[] bytes = e1.GetBytes("中国人民解放军"); //转为二进制

string str = Encoding.GetEncoding("UTF-8").GetString(bytes); //转回UTF-8编码 

四、文本文件操作 

string path = @"f:\t.txt";
//创建并写入(将覆盖已有文件)
if (!File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("Hello");
    }
}
//读取文件
using (StreamReader sr = File.OpenText(path))
{
    string s = "";
    while ((s = sr.ReadLine()) != null)
    {
        Console.WriteLine(s);
    }
}
//删除/拷贝
try
{
    File.Delete(path);
    File.Copy(path, @"f:\tt.txt");
}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());

} 

五、流文件操作 

string name = "Test.data";
//打开文件或通过File创建fs = File.Create(path, 1024)
FileStream fs = new FileStream(name, FileMode.CreateNew);
//转换为字节 写入数据(可写入中文)
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
//字节数组,字节偏移量,最多写入的字节数
fs.Write(info, 0, info.Length);
fs.Close();
//打开文件
fs = new FileStream(name, FileMode.Open, FileAccess.Read);
//读取
BinaryReader r = new BinaryReader(fs);
for (int i = 0; i < 11; i++)
{
    Console.WriteLine(r.ReadInt32());
}

fs.Close(); 

原文地址:https://www.cnblogs.com/sydeveloper/p/3023845.html