//删除.meta文件
string[] metaPaths = Directory.GetFiles(@"D:\TestMeta", "*.meta");
foreach (string metaPath in metaPaths)
{
File.Delete(metaPath);
}
//读取txt
using System.IO;
string[] line= File.ReadAllLines(@"D:\test.txt");
//遍历所有行
for (int i = 0; i < line.Length; i++)
{
Console.WriteLine(line[i]);
}
//读取版本文件 index
var targetVersionFilePath = "D:\test.txt"
if (File.Exists(targetVersionFilePath))
{
string[] lines = File.ReadAllLines(targetVersionFilePath);
if (lines.Length > 0)
{
var targetVersion = int.Parse(lines[0]);
}
}
//拷贝文件
File.Copy(sourceVersionFilePath, targetVersionFilePath, true);
//拷贝文件夹中的文件
public static void RecursiveCopyDirectory(string sourcePath, string targetPath, List<string> excludeExtensions = null, bool isOverwrite = true)
{
if (!Directory.Exists(sourcePath))
{
return;
}
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
var sourceDirectoryInfo = new DirectoryInfo(sourcePath);
var files = sourceDirectoryInfo.GetFiles();
foreach (var file in files)
{
if (null != excludeExtensions)
{
var fileExt = Path.GetExtension(file.Name);
var isFileExcluded = excludeExtensions.Contains(fileExt);
if (!isFileExcluded) continue;
}
try
{
file.CopyTo(Path.Combine(targetPath, file.Name), isOverwrite);
}
catch (System.Exception e)
{
Debug.LogError("e=" + e.Message);
}
}
var dirs = sourceDirectoryInfo.GetDirectories();
foreach (var dir in dirs)
{
RecursiveCopyDirectory(Path.Combine(sourcePath, dir.Name), Path.Combine(targetPath, dir.Name), excludeExtensions, isOverwrite);
}
}
c#文件操作1
于 2022-11-16 18:33:38 首次发布