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 - ] != 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

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

  1. 把ANSI格式的TXT文件批量转换成UTF-8文件类型

    把ANSI格式的TXT文件批量转换成UTF-8文件类型 Posted on 2010-08-05 10:38 moss_tan_jun 阅读(3635) 评论(0) 编辑 收藏 #region 把AN ...

  2. dos文件批量转换成unix文件

    对于经常在windows环境下和linux环境同时使用的文件(如在windows系统下编写,在linux环境下编译的文件), 常常存在这样的问题:由于两种系统的格式文件格式不同,导致程序出现不期望的问 ...

  3. SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text

    csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...

  4. C# winform 选择文件保存路径

    1.winform 点击按钮选择文件保存的路径,效果如下图: 具体代码如下: private void button8_Click(object sender, EventArgs e) { Fold ...

  5. C# winform选择文件、选择文件夹、打开文件

    文章来自博客园友,这里只是做一下笔记. 来源:https://www.cnblogs.com/liuqifeng/p/9149125.html 一.选择文件用OpenDialog OpenFileDi ...

  6. Java实现文件批量重命名

    Windows操作系统可以实现重命名文件操作,却不能实现批量重命名.本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规则批量重命名.用户可以给出重命名模板,程序可以根据模板对相应 ...

  7. 琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式

    记录日常工作常用到的一些方法: 1 选择文件操作,并将文件的路径记录下来: OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = f ...

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

    1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...

  9. C#选择文件、选择文件夹、打开文件

    1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...

随机推荐

  1. 工作中用的cobbler命令行

    在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...

  2. pandas-如何得到某一个值所在的行

    df[df['列名'].isin([相应的值])]

  3. 【图灵学院10】高并发之java线程池源码分析

    1. 提纲 1)线程池的模块结构 2)示例&原理解析 2. 问题 1)线程池包含哪些东西 2)线程池的运作原理 3)调度线程池的运作原理 4)线程池怎么实现FixRate,FixDelay,他 ...

  4. hadoop HDFS文件系统的特征

    hadoop HDFS文件系统的特征 存储极大数目的信息(terabytes or petabytes),将数据保存到大量的节点当中.支持很大单个文件. 提供数据的高可靠性,单个或者多个节点不工作,对 ...

  5. 一些意想不到的小bug。

    一,当if的时候,很容易忘记相对的else,从而出现bug,要将背面消息考虑全面. 二,多个元素在同一行布局的时候,要考虑文字的换行还是省略号代替. 例如:左边一个文字,宽度不固定,右边一个图形,宽度 ...

  6. Mysql安装(Mac)

    1.安装mysql(百度详解) 2.打开终端 3.输入vim ~/.bash_profile 4.在最后加上PATH=$PATH:/usr/local/mysql/bin 5.按esc,然后输入 :w ...

  7. USACO 1.1.1 YOUR RIDE IS HERE

    众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走.他们为每 ...

  8. oracle Date format日期和月份不补0

    格式化日期,日,月只有一位小数的只显示一位,不自动加0,在dd/mm 前面加上fm即可去掉前面的0 如:select to_char(t,'YYYY/fmMM/fmDD') from testdata ...

  9. asp.net core 简化模型验证 modelState.IsValid不用每一个写

    第一种:直接在执行action之前验证模型 实现 IActionFilter public class ModelStateFilter : IActionFilter { public void O ...

  10. Window安装TensorFlow- GPU环境

    [简述] 关于Window安装TensorFlow- GPU环境的文章我想网站已经有非常多了,但是为什么还要写这篇文章呢,就是被网上的文章给坑了.由于pip install tensorflow-gp ...