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 - ] != 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文件批量转编码 选择文件夹的更多相关文章
- 把ANSI格式的TXT文件批量转换成UTF-8文件类型
把ANSI格式的TXT文件批量转换成UTF-8文件类型 Posted on 2010-08-05 10:38 moss_tan_jun 阅读(3635) 评论(0) 编辑 收藏 #region 把AN ...
- dos文件批量转换成unix文件
对于经常在windows环境下和linux环境同时使用的文件(如在windows系统下编写,在linux环境下编译的文件), 常常存在这样的问题:由于两种系统的格式文件格式不同,导致程序出现不期望的问 ...
- SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text
csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...
- C# winform 选择文件保存路径
1.winform 点击按钮选择文件保存的路径,效果如下图: 具体代码如下: private void button8_Click(object sender, EventArgs e) { Fold ...
- C# winform选择文件、选择文件夹、打开文件
文章来自博客园友,这里只是做一下笔记. 来源:https://www.cnblogs.com/liuqifeng/p/9149125.html 一.选择文件用OpenDialog OpenFileDi ...
- Java实现文件批量重命名
Windows操作系统可以实现重命名文件操作,却不能实现批量重命名.本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规则批量重命名.用户可以给出重命名模板,程序可以根据模板对相应 ...
- 琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式
记录日常工作常用到的一些方法: 1 选择文件操作,并将文件的路径记录下来: OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = f ...
- C#选择文件、选择文件夹、打开文件(或者文件夹)
1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...
- C#选择文件、选择文件夹、打开文件
1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...
随机推荐
- CSS之引入样式
CSS引入样式 内部样式 内嵌式是将CSS代码集中写在HTML文档的head头部标签中,并且用style标签定义,其基本语法格式如下: <head> <style type=&quo ...
- ThreadLocalRandom原理
原文链接:https://www.jianshu.com/p/9c2198586f9b 2.2. 并发包中ThreadLocalRandom类原理剖析 ThreadLocalRandom类是JDK7在 ...
- Oracle数据控制语言(DCL)
数据控制语言 用来授予或者回收访问数据库中的各种对象,如表 .视图 .索引 等 基本的操作如下: --创建用户 CREATE USER jim IDENTIFIED BY changeit; --给用 ...
- CF796A Buying A House 模拟
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains ...
- Java内存区域与内存溢出异常---运行时数据区域
运行时数据区域 Java虚拟机所管理的内存将会包括以下几个运行时数据区域 线程私有区域 1.程序计数器 程序计数器记录的是当前正在执行的虚拟机字节码指令所在的地址.在虚拟机的概念模型中,字节码解释 ...
- servlet验证2
登录界面 登录成功后 数据库 地址:https://gitee.com/lgcj1218/j2eehomework/tree/master
- LUNA16数据集(二)肺结节可视化
在检测到肺结节后,还需要可视化,这样才能为诊断服务. 我使用的项目地址为:https://github.com/wentaozhu/DeepLung 项目基于论文:DeepLung: Deep 3D ...
- [ZJOI2018]历史(LCT)
这篇还发了洛谷题解 [Luogu4338] [BZOJ5212] 题解 题意 给出一棵树,给定每一个点的 \(access\) 次数,计算轻重链切换次数的最大值,带修改. 先考虑不带修改怎么做 假设 ...
- Codeforces Round #335 (Div. 2) C
C. Sorting Railway Cars time limit pe ...
- Java的JsonHelper
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <g ...