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. Java编程思想读书笔记之一切皆对象

    一切皆对象 Java程序运行时,数据保存到哪里 寄存器 这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部.然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配.我们对此没 ...

  2. “网易大数据讲堂第一期:数说”直播活动资料:课程回放收看及PPT下载

    欢迎访问网易云社区,了解更多网易技术产品运营经验. "网易大数据讲堂第一期:数说"直播活动昨晚顺利举行.感谢各位"数"友的支持和参与. 本次活动PPT可点击这里 ...

  3. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  4. C#之重载

    前言 今天去看了看论坛,发现有些人对于重载还是很有疑问的,像大多数人一样,貌似知道重载方法这回事儿, 但是具体怎么应用,或者用重载方法的好处,好像还是一知半解,模模糊糊.我们都知道,重载方法的定义,甚 ...

  5. 阅读GFS的一点总结

    这是我第一次阅读学术论文,文章中充斥的一些学术名词给我的阅读带来了一些困难,因为此前没有接触过这方面的内容,在同学的帮助下,查阅了一些资料,终于对GFS有了一点认识,写出这一些感悟,文章措辞不严谨之处 ...

  6. pytorch搭建网络,保存参数,恢复参数

    这是看过莫凡python的学习笔记. 搭建网络,两种方式 (1)建立Sequential对象 import torch net = torch.nn.Sequential( torch.nn.Line ...

  7. SwiftMailer 发送邮件时 提示fsockopen() 被禁用

    站点转移空间,发送邮件的SwiftMailer 类提示错误如下: Warning: fsockopen() has been disabled for security reasons in D:\1 ...

  8. C语言单片机中延时程序的实现

    在单片机或嵌入式系统的程序,常常用规定次数的空循环来实现延时 /** * 通过一个空循环体循环让程序运行一段时间.在嵌入式系统中,这个函数用来实现延时. * * 参数: *    u16 i -- 循 ...

  9. Codeforces-B-Game with string(模拟栈)

    Two people are playing a game with a string ss, consisting of lowercase latin letters. On a player's ...

  10. day_09 函数及参数

    1.定义:把功能封装起来,方便下次直接调用 2.语法:def 关键词开头,空格之后接函数名称和圆括号(). def 函数名(形参) 函数体 3.参数:圆括号用来接收参数.若传入多个参数,参数之间用逗号 ...