c# IO处理与Path

1.DirectoryInfo

//获取一个路径下的文件信息
directoryInfo = new DirectoryInfo(path);  
//获取该文件路径下的所有子文件夹   --只是子文件夹  子辈一下文件夹无法查找
directoryInfo.GetDirectories() 
//获取该文件路径下所有文件
FileInfo[] files = directoryInfo.GetFiles(suffix); 

2.File,Exists(Path) --查询指定路径

if (!File.Exists(JournalPath))  //查找是否存在该路径 返回bool
{
 Directory.CreateDirectory(JournalPath);
}

3.Directory.CreateDirectory --创建文件夹

Directory.CreateDirectory(JournalPath); //创建一个路径文件夹

4.File.Create --创建文件

//创建一个路径文件(包含后缀)  dispose()立即处理生成
File.Create(path).Dispose()

5.new StreamWriter(fileName,true); --IO 写入流

 //创建一个写流   构造函数(文件路径,可选(true时写入内容会在原有内容下写入,false或不写写入内容会覆盖原有内容))
 StreamWriter textureStream = new StreamWriter(fileName,true);

6.获取文件大小(字节)

 long size;
 void GetFileSize()
 {
     //获取路径文件信息
     DirectoryInfo di = new DirectoryInfo(assetPath); 
     freach (FileInfo fi in di.GetFiles())  //遍历
     {
          //获取文件长度(也就是size)
         size += fi.Length; 
     }
 }

7.Path.Combine --路径合并

//合并路径 支持不同的分隔符
(http://path.combine(application.datapath/), "Scripts/AssetPostProcessor");
static string RootPath = [Path.Combine(Application.dataPath]

8.Path.GetFullPath --获取一个子路径的完整路径

//assetPath为Assets下路径  通过方法获取该文件完整路径
//!!!该方法还有很多妙用,暂时还不太清楚具体用处
fullPath= Path.GetFullPath(assetPath);

9.Path.GetDirectoryName--获取该路径文件的父目录

//assetPath = Assets/Scripts/AssetPostProcessor/text_go 12.jpg
//hh = Assets/Scripts/AssetPostProcessor
string hh = Path.GetDirectoryName(assetPath);

10.Path.GetExtension --获取该路径文件的文件类型

//assetPath = Assets/Scripts/AssetPostProcessor/text_go 12.jpg
//pp = ".jpg"
string pp = Path.GetFileNameWithoutExtension(assetPath);

11.Path.GetFileNameWithoutExtension --获取该路径文件名字(不含后缀)

//assetPath = Assets/Scripts/AssetPostProcessor/text_go 12.jpg
//pp = "text"
string pp = Path.GetFileNameWithoutExtension(assetPath);

12.Path.GetPathRoot --获取路径的绝对根目录(比如C:\ D:\)

//fullpath = D:\\SandBox\\trunk\\public\\res\\editor\\Assets\\Scripts\\AssetPostProcessor\\text_go 5.jpg
//root = D:\\    没有绝对父目录是返回""
string root = Path.GetPathRoot(fullPath);

13.Path.GetTempFileName() --创建唯一命名的临时文件 返回完整路径

//r = C:\\Users\\Youcai\\AppData\\Local\\Temp\\tmp7448c363.tmp
string r = Path.GetTempFileName();

14.Path.GetTempPath(); --获取创建临时文件的父目录

//r1 = C:\\Users\\Youcai\\AppData\\Local\\Temp\\
string r1 = Path.GetTempPath();

15.Path.HasExtension() --检测路径是否包含扩展名 返回bool

//检测路径下是否还有包含拓展名文件 
//路径内文件包括.并且后跟一个或多个字符 返回true 否则返回false
bool   r2= Path.HasExtension(assetPath);

16.Path.IsPathRooted() --检测路径是否包含绝对根目录 返回bool

//检测路径是否包含绝对根目录
bool   r3 = Path.IsPathRooted(fullPath);

你可能感兴趣的:(c# IO处理与Path)