C#扫描文件夹中中文文件

2023-08-24 13:58:38 浏览数 (2)

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace CheckChinese
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("是否删除中文文件:Y/N");
            string yOrNo = "";
            yOrNo = Console.ReadLine().ToString();
                
            var dir = Directory.GetCurrentDirectory();
            foreach (var f in new DirectoryInfo(dir).GetFiles("*.*", SearchOption.AllDirectories))
            {
                if (IsHaveChianese(f.FullName))
                {
                    Console.WriteLine(f.FullName);
                    if ( yOrNo == "Y" || yOrNo == "y")
                    {
                        File.Delete(f.FullName);
                    }
                } 
            }
            Console.WriteLine("按enter结束");
            Console.ReadLine();
        }

        public static bool IsHaveChianese(string path)
        {

            string fileName = Path.GetFileName(path);
            string filePath = Path.GetDirectoryName(path);
            filePath = Path.GetFileName(filePath);
            if (Regex.Match(path, "[u4e00-u9fa5]").Success)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

1.有时候unity中会不小心导入一些中文文件 2.把生成的exe放到assets文件中,遍历扫描assets文件夹下中文文件

0 人点赞