C# winform文件批量转编码 选择文件夹

C# winform文件批量转编码 选择文件夹 打开指定目录

private void btnFile_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择文件";
fileDialog.Filter="所有文件(*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file=fileDialog.FileName;
MessageBox.Show("已选择文件:" + file,"选择文件提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}

private void btnPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
string foldPath = dialog.SelectedPath;
MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnOpen_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Explorer.exe","c:\\windows");
}

批量文件转编码

        #region 文件批量转编码

        #region 文件后缀名集合

        //文件后缀名
        private List<string> _fileExtension;

        /// <summary>
        /// 文件后缀名
        /// </summary>
        public List<string> FileExtension
        {
            get { return _fileExtension; }
            set { _fileExtension = value; }
        }

        #endregion

        /// <summary>
        /// 文件批量转编码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeEncoding_Click(object sender, EventArgs e)
        {
            try
            {
                string strExtension = this.txtExtension.Text.Trim();
                if (!string.IsNullOrWhiteSpace(strExtension))
                {
                    FileExtension = strExtension.ToLower().Split(new char[] { ‘,‘, ‘,‘},StringSplitOptions.RemoveEmptyEntries).ToList();
                }
                else
                {
                    MessageBox.Show("输入文件后缀名!");
                    return;
                }
                string sourceDirName = this.txtSourceFoldPath.Text.Trim();
                if (string.IsNullOrWhiteSpace(sourceDirName))
                {
                    MessageBox.Show("请选择源文件夹!");
                    return;
                }
                string destDirName = this.txtDestFoldPath.Text.Trim();
                if (string.IsNullOrWhiteSpace(destDirName))
                {
                    MessageBox.Show("请选择目标文件夹!");
                    return;
                }
                CopyDirectory(sourceDirName, destDirName);
                MessageBox.Show("转换成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换失败!" + ex.ToString());
                throw;
            }

        }

        #region 转文件编码

        /// <summary>
        /// 文件转换编码
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="destFile">目标文件</param>
        /// <param name="targetEncoding">编码</param>
        private void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding)
        {
            destFile = string.IsNullOrWhiteSpace(destFile) ? sourceFile : destFile;
            string strSource = System.IO.File.ReadAllText(sourceFile, System.Text.Encoding.Default);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(destFile, false, targetEncoding);
            sw.WriteLine(strSource);
            sw.Close();
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="sourceDirName">源文件</param>
        /// <param name="destDirName">目标文件</param>
        private void CopyDirectory(string sourceDirName, string destDirName)
        {
            try
            {
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                    System.IO.File.SetAttributes(destDirName, System.IO.File.GetAttributes(sourceDirName));
                }

                if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar) //  =="\\"
                {
                    destDirName = destDirName + Path.DirectorySeparatorChar;
                }
                string[] files = Directory.GetFiles(sourceDirName);
                foreach (string file in files)
                {
                    if (System.IO.File.Exists(destDirName + Path.GetFileName(file)))
                        continue;
                    //转编码复制
                    if (FileExtension.Contains(Path.GetExtension(file).ToLower()))
                    {
                        ConvertFileEncoding(file, destDirName + Path.GetFileName(file), System.Text.Encoding.UTF8);
                        System.IO.File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
                        //total++;
                    }
                    //System.IO.File.Copy(file, destDirName + Path.GetFileName(file), true);
                    //System.IO.File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
                    ////total++;
                }

                string[] dirs = Directory.GetDirectories(sourceDirName);//包括路径
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, destDirName + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {
                StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\log.txt", true);    //System.Web.Providers.Entities
                sw.Write(ex.Message + "     " + DateTime.Now + "\r\n");
                sw.Close();
                throw;
            }

        }

        /// <summary>
        /// 目标文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDestFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdlgSource = new FolderBrowserDialog();
            if (fbdlgSource.ShowDialog() == DialogResult.OK)
            {
                this.txtDestFoldPath.Text = fbdlgSource.SelectedPath;
            }
        }

        /// <summary>
        /// 源文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSourceFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdlgSource = new FolderBrowserDialog();
            if (fbdlgSource.ShowDialog() == DialogResult.OK)
            {
                this.txtSourceFoldPath.Text = fbdlgSource.SelectedPath;
            }
        }

        #endregion

        #endregion

时间: 2024-08-10 21:23:09

C# winform文件批量转编码 选择文件夹的相关文章

TXT文件批量换成HTML文件

首先,看下100个txt文件. 我们新建一个txt,输入 ren *.txt *.html 保存,并修改文件名,修改后缀为1.bat.并且双击bat文件. 见证奇迹的时刻到了,所有的txt文本后缀全部替换成为html后缀的. 5 操作都比较简单啊,主要在于批量二字,能替代人工省了不少事情.

C#选择文件、选择文件夹、打开文件(或者文件夹)

转载自:http://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowD

C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!

https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowDial

通编码读取文件内容

通编码读取文件内容 # 通编码读取文件内容 def read_lines_from_file(file_path, coding="utf-8"): line_content = [] if os.path.isfile(file_path): try: with open(file_path, encoding=coding) as fp: line_content = fp.readlines() return line_content except Exception as e:

C#选择文件(OpenFileDialog)、选择文件夹(FolderBrowserDialog)

1  选择文件(OpenFileDialog) OpenFileDialog dialog = new OpenFileDialog(); //dialog.Multiselect = true;//该值确定是否可以选择多个文件 // dialog.Title = "请选择文件"; //dialog.Filter = "所有文件(*.*)|*.*"; //dialog.InitialDirectory = @"E:\"; if (dialog.S

C# winform 选择文件保存路径

winform 点击按钮选择文件保存的路径,效果如下图: 具体代码如下: private void button8_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { string foldPath =

c#winform选择文件,文件夹,打开指定目录方法

private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter="所有文件(*.*)|*.*"; if (fileDialog.ShowDialog() == Dia

Winform选择目录路径与选择文件路径

https://blog.csdn.net/zaocha321/article/details/52528279 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public par

【分享】Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件

[分享]Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件 在Andoird Studio下编译java文件时,经常会出现像下面的错误: Error:(29, 43) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 在这里,分享一个工具:ANSI文件批量另存为无BOM的UTF-8文件: 把下面代码用记事本存为AndroidStudioJava编码.vbs,双击即可使用: on error resume next Set WshShell=WScrip