C#遍历某目录及其子目录下所有文件的一个例子

问题源于游戏汉化,由于要对汉化文本做某一种批处理,而这些文本集中在某一目录及其各层子目录中,于是如何遍历就成为了一个问题。

直接上代码,不过多解释了,只看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();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值