• 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");
    }
    View Code

    批量文件转编码

            #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 + "
    ");
                    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
    View Code
  • 相关阅读:
    读写锁
    MySQL事务处理和锁机制
    SQL注入攻击
    数据库三范式
    Slave延迟很大的优化方法总结(MySQL优化)
    MySQL主从复制的原理及配置
    消息总线的应用场景
    Java NIO通信框架在电信领域的实践
    逃逸分析
    BOM
  • 原文地址:https://www.cnblogs.com/love201314/p/6758167.html
Copyright © 2020-2023  润新知