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;//该值确定是否可以 ...
随机推荐
- Repeater控件的分隔线
在Repeater控件中,很容易实现在行与行之间使用分隔线.因为此控件有内置的SeparatorTemplate模版.举个例子吧: 运行时,可以看到效果: 说句实在的话,Insus.NET做一条水平线 ...
- JavaWeb中MVC的使用--以管理系统举例
开发环境:JavaSE1.7.JavaEE7.0.JSTL1.2.2.Web2.3.MySQL5.5.28 系统分析与功能设计: 本系统实现商品信息的管理,应包括以下几个功能: 商品信息列表:列出所有 ...
- cenos 上的php 支持GD库问题
---恢复内容开始--- thinkphp 开发的项目verify类无法引用,原因是没有开启gd库 环境:CentOS 6.4,php-5.3.3需求:php支持GD库解决方案:GD是Linux下的一 ...
- CF959D Mahmoud and Ehab and another array construction task 数学
Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...
- django内置服务器
单进程多线程的 多线程用来并发,各个线程之间不会阻塞,每个线程对应一个连接
- day17 isinstance type issubclass 反射
1. issubclass,type,isinstance 1.issubclass 判断xxx是否yyy的子类 例: class Foo: pass class Bar(Foo): pass cla ...
- http协议&接口规范&接口测试入门
http协议 请求: 请求行:请求方法.url(协议名://ip;端口/工程名/资源路径).协议版本 请求头 :键值对 请求正文 响应: 响应行:协议版本.响应状态码.响应状态码描述 响应头 :键值对 ...
- Python 基础学习之if语句
cars=['audi','xiali','bwm','benz',] ##根据要求bmw全部为大写,其他的首字母为大写 for car in cars: if car=='bmw': print(c ...
- 匿名类与lambda区别
第一种是继承Thread, 重写了Thread.run() getClass()返回的是匿名类 java.long.Thread$1 第二种是lambda, 重写了Runnable.run() ...
- python升级到3.*版本
转载https://blog.csdn.net/liang19890820/article/details/51079633 简述 CentOS 7 中默认安装了 Python,版本比较低(2.7.5 ...