• 将文本文件转换成UTF8格式


    复制文件:

    protected void CopyAll(string olddirectory, string newdirectory, string dir)
    {
        DirectoryInfo info = new DirectoryInfo((dir.Length > 0) ? dir : olddirectory);
        FileInfo[] files = info.GetFiles();
        foreach (FileInfo info2 in files)
        {
            try
            {
                string destFileName = string.Format(@"{0}\{1}", (dir.Length > 0) ? dir.Replace(olddirectory, newdirectory) : newdirectory, info2.Name);
                File.Copy(info2.FullName, destFileName);
                FileInfo info3 = new FileInfo(destFileName);
                info3.Attributes &= FileAttributes.ReadOnly;
                byte[] bytes = File.ReadAllBytes(destFileName);
                string str2 = Encoding.UTF8.GetString(bytes);
                File.WriteAllText(destFileName, File.ReadAllText(destFileName, Encoding.Default), this.UTF8WithOutBom);
                info3.Attributes |= FileAttributes.ReadOnly;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
        if (info.GetDirectories().Length > 0)
        {
            for (int i = 0; i < info.GetDirectories().Length; i++)
            {
                string path = string.Format(@"{0}\{1}", (dir.Length > 0) ? dir.Replace(olddirectory, newdirectory) : newdirectory, info.GetDirectories()[i].Name);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                this.CopyAll(olddirectory, newdirectory, info.GetDirectories()[i].FullName);
            }
        }
    }
    

     

    复制文件:

    public void CopyDirectoryAndFiles(string olddirectory, string newdirectory)
    {
        DirectoryInfo info = new DirectoryInfo(newdirectory);
        if (!info.Exists)
        {
            Directory.CreateDirectory(newdirectory);
        }
        this.CopyAll(olddirectory, newdirectory, "");
    }
    

      

    转换:

    private void button1_Click(object sender, EventArgs e)
    {
        string text = this.textBox1.Text;
        string newdirectory = this.textBox2.Text;
        this.CopyDirectoryAndFiles(text, newdirectory);
        MessageBox.Show("finished");
    }
    
     
    

      

  • 相关阅读:
    党务
    平台 大赛 公司
    音乐
    有趣的博主
    C++获取命令行参数命令
    360读全景
    3 海康网络相机官方例程(3)OpenCv + ffmpeg + rtmp 实现摄像头采集数据直播功能(不带cuda加速)
    使用opencv4进行分类器训练
    经典环境(2)OpenCV412+OpenCV-Contrib +vs2015+cuda10.1编译
    经典环境(1)OpenCV3.4.9+OpenCV-Contrib +vs2015+cuda10.1编译
  • 原文地址:https://www.cnblogs.com/wzihan/p/15583984.html
Copyright © 2020-2023  润新知