问题源于游戏汉化,由于要对汉化文本做某一种批处理,而这些文本集中在某一目录及其各层子目录中,于是如何遍历就成为了一个问题。
直接上代码,不过多解释了,只看checkFiles函数就行,其他和遍历没关,是用来处理文本的,我懒得删了,不用看。checkFile(file.FullName)直接写成system.console.out.writeLine(file.FullName)就是打印所有文件的绝对路径了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace HarukaReplace
{
class Program
{
static void Main(string[] args)
{
//checkFile("D:/测试用/中文/EV/中ボス敗北ED/2人.txt");
checkFiles(new DirectoryInfo("D:/超昂闪忍文本处理程序/中文"));
say("Completed!");
}
static void checkFiles(FileSystemInfo info)
{
if (!info.Exists)
{
say("文件夹不存在!");
return;
}
DirectoryInfo dir = info as DirectoryInfo;
if (dir == null)
{
say("所选目标不是文件夹!");
return;
}
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null)
checkFile(file.FullName);
else
checkFiles(files[i]);
}
}
static void checkFile(String filePath)
{
String chsOrgString = System.IO.File.ReadAllText(filePath);
int temp = chsOrgString.IndexOf("m 000");
if (temp == -1) return;
String chsSubString1 = chsOrgString.Substring(0, temp);
String chsSubString2 = chsOrgString.Substring(temp);
String judge = chsSubString2.Substring(0, chsSubString2.IndexOf("\r\n"));
if (judge.IndexOf("/") == -1 && judge.IndexOf("FUNCTION") == -1 && judge.IndexOf("void") == -1) return;
chsSubString2 = chsSubString2.Substring(chsSubString2.IndexOf("\r\n"));
String jpFilePath = filePath.Substring(0, filePath.IndexOf("中文")) + "日文" + filePath.Substring(filePath.IndexOf("中文") + 2);
String jpOrgString = System.IO.File.ReadAllText(jpFilePath);
jpOrgString = jpOrgString.Substring(jpOrgString.IndexOf("m 000"));
jpOrgString = jpOrgString.Substring(0, jpOrgString.IndexOf("\r\n"));
chsOrgString = chsSubString1 + jpOrgString + chsSubString2;
StreamWriter sw = new StreamWriter(filePath);
sw.Write(chsOrgString);
sw.Flush();
sw.Close();
}
static void say(String whatToSay)
{
System.Console.Write(whatToSay);
System.Console.ReadKey();
}
}
}