//c#修改文件夹为ansi编码,获取当前文件夹里的所有文件夹。刚写的!
[C#] 纯文本查看 复制代码 using System;
using System.Text;
using System;
using System.IO;
using System.Text;
public class UnicodeToAnsiConverter
{
public static void Main()
{
string folderPath = Directory.GetCurrentDirectory(); // 替换为你要处理的文件夹路径
Console.WriteLine(folderPath);
string[] subfolders = Directory.GetDirectories(folderPath);
foreach (string subfolder in subfolders)
{
// 获取子文件夹名称
string folderName = Path.GetFileName(subfolder);
Console.WriteLine("Original Unicode Folder Name: " + folderName);
string newname = "";
string testString = folderName; // 一个包含中文字符的字符串
foreach (char c in testString)
{
// 判断字符是否为Unicode编码
bool isUnicode = IsUnicode(c);
if (isUnicode)
{
byte[] unicodeBytes = Encoding.Unicode.GetBytes(c.ToString());
byte[] gbkBytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding("GBK"), unicodeBytes);
string gbkChar = Encoding.GetEncoding("GBK").GetString(gbkBytes);
Console.WriteLine($"The Unicode character '{c}' converted to GBK: {gbkChar}");
newname = newname + gbkChar;
}
else
{
Console.WriteLine($"The character '{c}' is not Unicode.");
}
// Console.ReadLine();
}
//Console.ReadLine();
//改名:
try
{
Directory.Move(folderPath + "\\"+folderName, folderPath+"\\"+ newname);
Console.WriteLine("文件夹已成功重命名。");
}
catch (Exception ex)
{
Console.WriteLine("重命名文件夹时发生错误: " + ex.Message);
}
}
Console.ReadLine();
}
public static bool IsUnicode(char c)
{
// UTF-16 Unicode范围是0x0000到0xFFFF
return (c >= 0x0000 && c <= 0xFFFF);
}
}
|