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. spring分布式事务学习笔记(2)

    此文已由作者夏昀授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Model类如下:package com.xy.model 1 package com.xy.model;   ...

  2. C++ 打印缓存区数据 十六进制格式

    1.调试C++程序时,有时可能需要以16进制打印输出缓存区数据,以定位跟踪问题,现提供其实现的代码: void printHex(char* buff, int buff_len) { + ); if ...

  3. Glib学习笔记(一)

    你将学到什么 如何使用GObject实现一个新类 类头文件 声明一个类型的方法选择取决于类型是可被继承的还是不可被继承的. 不可被继承的类型(Final类型)使用G_DECLARE_FINAL_TYP ...

  4. 洛谷 P2447 [SDOI2010]外星千足虫

    P2447 [SDOI2010]外星千足虫 题目描述 公元2089年6月4日,在经历了17年零3个月的漫长旅行后,“格纳格鲁一号”载人火箭返回舱终于安全着陆.此枚火箭由美国国家航空航天局(NASA)研 ...

  5. [翻译]CURAND Libaray--Host API--(1)

    原文来自:cuda curand toolkit document Translated by xingoo 如果有错误请联系:xinghl90@gmail.com 2Host API简述 使用hos ...

  6. [Oracle入门级]知识概况

    oracle各个版本间的主要技术更新 oracle 增加数据库创建和存储对象 oracle 8i 整体性能提升 oracle9i 实施应用集群 oracle 10g 支持网格计算 oracle 11g ...

  7. iOS10 新特性-新功能,以及ReplayKit库

    iOS的10.0 本文总结了iOS的10,运行于目前推出iOS设备推出的主要开发者相关的功能. iOS版10引入了新的方法来增加您的应用程序通过帮助系统参与在适当的时候建议你的应用程序给用户.如果你在 ...

  8. Kibana6.x.x——导航权限控制入门

    按如下图所示设置: 用该用户登录后,界面如图所示: 但遗憾的是,根据官方论坛的说法,其它的导航隐藏控制,暂时还不支持. 参考:https://discuss.elastic.co/t/hide-ina ...

  9. SQL-Error-1

    错误描述:String   or   binary   data   would   be   truncated. 原因:字段长度不够

  10. 安装openstack时遇到的错误

    学习opensatck的第一步是安装DevStack来进行本机操作 1. 下面命令没有权限,解决办法:切换到root用户下执行sudo -s echo "stack ALL=(ALL) NO ...