将UTF-8转换为ANSI并重命名文件夹中的文件

时间:2016-12-11 14:29:55

标签: c# encoding directory rename

我想将一个文件夹中的文件转换为另一种编码器(ANSI)格式,我想删除字符'。' (例如:11.12.2016.txt - > 11122016.txt)

我为单个文件写了一个函数,一切都还可以,但我想制作所有文件夹,当我尝试这个时它给我这个过程使用错误。

这是我的代码 - 谢谢!

public void ConvertToANSI()
{
    try
    {
        DirectoryInfo DI = new DirectoryInfo(@"C:\Users\Ozan-Gaming\Desktop\deneme2");
        FileInfo[] Files = DI.GetFiles("*.txt");

        try
        {
            foreach (FileInfo Fl in Files)
            {
                StreamReader SR = Fl.OpenText();  // this opens a stream to the file **in UTF8 encoding**

                StreamWriter SW = new StreamWriter(new FileStream(Fl.FullName, FileMode.OpenOrCreate), Encoding.GetEncoding(1250));
                SW.Write(SR.ReadToEnd());
            }
        }
        catch (Exception)
        {
        }

        // Renaming Starting
        DirectoryInfo DI2 = new DirectoryInfo(@"C:\Users\Ozan-Gaming\Desktop\deneme2");
        FileInfo[] Files2 = DI2.GetFiles("*.txt");

        foreach (FileInfo f in Files2)
        {
            var charsToRemove = new string[] {"."};
            string fullname = f.FullName;

            foreach (var c in charsToRemove)
            {
                fullname = fullname.Replace(c, string.Empty);
            }

            File.Move(f.FullName, fullname);
        }

        lblSonuc.Text = "OK";
    }
    catch (Exception ex)
    {
        lblSonuc.Text = "HATA : "+ex.Message;
        throw;
    }
}

0 个答案:

没有答案