标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication3
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
cbb_xuanze.SelectedIndex = 0;
}
private void btn_Choice_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();//选择路径
//folderBrowserDialog1.Multiselect = true;
text_Choice.Text = folderBrowserDialog1.SelectedPath;
//btn_TiHuan.Enabled = true;
//btn_guize.Enabled = true;
}
private void btn_TiHuan_Click(object sender, EventArgs e)
{
Execute(1);
shuaxin();
}
private void btn_guize_Click_1(object sender, EventArgs e)
{
Execute(2);
shuaxin();
}
private void Execute(int click)
{
string strlujing = text_Choice.Text.Trim();
string strold = text_old.Text;
string strnew = text_new.Text;
string strchong = text_chong.Text;
//由于原来一直都没注意到这两个方法,一直使用string.IsNullOrEmpty,当看到string.IsNullOrWhiteSpace时,而且在微软人员开发的项目中经常使用时才注意到,查了一下MSDN,记一下免得以后忘记。
//string.IsNullOrEmpty
//都知道,这个功能是判断字符串是否为:null或者string.Empty。如果是如"\t"这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了如下的方法。
//string.IsNullOrWhiteSpace
//这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的。根据MSDN的说明,这个方法会比调用上述两个方法的性能更高而且简洁,所以在判断这个功能时,推荐使用。
if (string.IsNullOrWhiteSpace(strlujing))
{
MessageBox.Show("请选择文件夹!");
}
else if (!Directory.Exists(strlujing))//确定给定路径是否引用磁盘上的现有目录,返回值为bool,如果 path 引用现有目录,则为 true;否则为 false。
{
MessageBox.Show("所指定的文件夹不存在!");
}
else
{
int num = 0;
try
{
string[] wenjian = Directory.GetFiles(strlujing);
//返回指定目录中与指定的搜索模式匹配的文件的名称(包含它们的路径)。参数类型:System.String要搜索的目录。
//返回值
//类型:System.String[]
//指定目录中与指定的搜索模式匹配的文件的全名(包含它们的路径)的数组。
pb_JinDuTiao.Maximum = wenjian.Length;
StringBuilder way = new StringBuilder();//拼接字符串 类似于+= 用来定义文件名
//StringBuilder 上的主要操作是 append 和 insert 方法。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符添加或插入到字符串生成器中。append 方法始终将这些字符添加到生成器的末端;而 insert 方法则在指定的点添加字符。
int i = 0;//成功操作个数。
if (wenjian == null || wenjian.Length == 0)
{
MessageBox.Show("文件夹中不存在文件!");
//MessageBoxButtons.OK:消息框包含“确定”按钮。MessageBoxButtons指定若干常数,用以定义 MessageBox 上将显示哪些按钮;
//MessageBoxIcon指定定义哪些信息要显示的常数。: MessageBoxIcon.Asterisk该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的。
}
else
{
DateTime StartTime = DateTime.Now;
foreach (string fileName in wenjian)
{
pb_JinDuTiao.Value = pb_JinDuTiao.Value + 1;
var fileinfo = new FileInfo(fileName);
//可以赋予局部变量推断“类型”var 而不是显式类型。 var 关键字指示编译器根据初始化语句右侧的表达式推断变量的类型。 推断类型可以是内置类型、匿名类型、用户定义类型或 .NET Framework 类库中定义的类型。
//提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 FileStream 对象。无法继承此类。
//将 FileInfo 类用于典型的操作,如复制、移动、重命名、创建、打开、删除和追加到文件。
//许多 FileInfo 方法在您创建或打开文件时返回其他 I/O 类型。可以使用这些其他类型进一步操作文件。有关更多信息,请参见特定的 FileInfo 成员,如 Open、OpenRead、OpenText、CreateText 或 Create。
//如果打算多次重用某个对象,可考虑使用 FileInfo 的实例方法,而不是 File 类的相应静态方法,因为并不总是需要安全检查。
//默认情况下,将向所有用户授予对新文件的完全读/写访问权限。
int index = fileinfo.Name.LastIndexOf(‘.‘);
way.Length = 0;
way.Append(fileinfo.DirectoryName + @"\");
switch (click)
{
case 1://替换
//文件名存在才进行替换。
if (strold == null || strold.Length == 0)
{
return;
}
if (fileinfo.Name.Substring(0, index > -1 ? index : fileinfo.Name.Length).Contains(strold))
{
way.Append(fileinfo.Name.Substring(0, index > -1 ? index : fileinfo.Name.Length).Replace(strold, strnew));
if (index > -1)
{
way.Append(fileinfo.Name.Substring(index, fileinfo.Name.Length - index));
}
fileinfo.MoveTo(Convert.ToString(way));
i++;
}
break;
case 2:
//if (cbb_xuanze.SelectedIndex != 0)
//{
// MessageBox.Show("此功能尚未完成!");
// pb_JinDuTiao.Value = 0;
// num = 0;
// return;
//}
switch (cbb_xuanze.SelectedIndex)
{
case 0:
if (rdb_qian.Checked == true)
{
way.Append((++num) + strchong);
}
else
{
way.Append(strchong + (++num));
}
if (index > -1)
{
way.Append(fileinfo.Name.Substring(index, fileinfo.Name.Length - index));
}
break;
case 1:
if (rdb_qian.Checked == true)
{
}
else
{
}
break;
}
fileinfo.MoveTo(Convert.ToString(way));//将指定文件移到新位置,并提供指定新文件名的选项。参数:要将文件移动到的路径,可以指定另一个文件名。
i++;
break;
}
}
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
Text = ("总耗时:" + ts.Minutes + "分" + ts.Seconds + "秒" + ts.Milliseconds + "毫秒");
MessageBox.Show("一共操作了:" + i + "个文件");
pb_JinDuTiao.Value = 0;
num = 0;
}
}
catch (Exception ex)
{
MessageBox.Show("错误信息:" + ex.Message, "错误");
pb_JinDuTiao.Value = 0;
num = 0;
}
}
}
private void button1_Click_1(object sender, EventArgs e)
{
shuaxin();
}
public void shuaxin()
{
List<string> st = new List<string>();
foreach (string file in Directory.GetFiles(text_Choice.Text))//获取指定目录下的文件名称,不包括文件夹
{
st.Add(file);
}
listView1.Columns.Clear();
listView1.Columns.Add("文件名", 600);
listView1.Items.Clear();
for (int i = 0; i < st.Count; i++)
{
//listView1.Items.Add(new ListViewItem(new string[] { st[i] }));
listView1.Items.Add(st[i].ToString());
}
}
private void text_Choice_TextChanged(object sender, EventArgs e)
{
if (text_Choice.Text == null || text_Choice.Text.Length == 0 || text_Choice.Text != folderBrowserDialog1.SelectedPath)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label3 .Text = DateTime.Now.ToLongTimeString();
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
textBox1.Text = folderBrowserDialog1.SelectedPath;
}
public void meiju(ListView al)
{
if (!string.IsNullOrWhiteSpace(textBox1.Text))//判断文本框中是否有内容
{
List<string> st = new List<string>();
foreach (string file in Directory.GetFiles(textBox1.Text))//获取指定目录下的文件名称,不包括文件夹 Directory.GetFiles(textBox1.Text) 为一个数组文件
{
st.Add(file);
//MessageBox.Show(file);
}
al.Columns.Clear();
al.Columns.Add("文件名", 600);
al.Items.Clear();
for (int i = 0; i < st.Count; i++)
{
al.Items.Add(st[i].ToString());//遍历文件夹内文件
}
}
}
private void button2_Click(object sender, EventArgs e)
{
meiju(listView1);
}
private void button3_Click(object sender, EventArgs e)
{
dandata();
meiju(listView1);
}
private void button4_Click(object sender, EventArgs e)
{
duodate();
meiju(listView1);
}
private void button5_Click(object sender, EventArgs e)
{
quandate();
meiju(listView1);
}
public void RenameDirectories(string OldPathName, string newPathName)
{
int i = 1;
string[] sDirectories = Directory.GetDirectories(OldPathName);
foreach (string sd in sDirectories)
{
string sdName = Path.GetFileName(sd);//获取文件的名称,没有路径,直接是文件名称
string sNewName = string.Format(newPathName, i++);//替换,string.Format(a{0},b{1},p1,p2) 输出结果为 a p1,b p2
string sNewDirectory = Path.Combine(OldPathName, sNewName);//将两个字符串合并
Directory.Move(sd, sNewDirectory);//重命名文件或移动文件到新的位置
}
}
public void dandata()
{
if (listView1.SelectedItems != null)
{
string path = listView1.SelectedItems[0].Text;//选中的项
string fname = Path.GetDirectoryName(path);//获取选中文件所在的目录路径
string bname = Path.GetFileName(path);//获取选中文件的文件名称(包含后缀如.txt)
string kname = Path.GetExtension(path);//获取选中文件的后缀名称
string OldStr = fname + @"\" + bname;
string NewStr = fname + @"\" + textBox2.Text + kname;
FileInfo fi = new FileInfo(OldStr);
fi.MoveTo(NewStr);//将制定文件移动到新位置,并提供新的文件名
}
}
public void duodate()
{
List<string> lt = new List<string>();
if (listView1.SelectedItems != null)
{
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
string sp = listView1.SelectedItems[i].Text;
lt.Add(sp);
}
for (int i = 0; i < lt.Count; i++)
{
string path = lt[i];
string OldStr = Path.GetDirectoryName(path) + @"\" + Path.GetFileName(path);
string NewStr = Path.GetDirectoryName(path) + @"\" + textBox2.Text + i.ToString() + Path.GetExtension(path);
FileInfo fi = new FileInfo(OldStr);
fi.MoveTo(NewStr);//将制定文件移动到新位置,并提供新的文件名
}
}
}
public void quandate()
{
int i = 1;
if (Directory.GetFiles(textBox1.Text) != null)
{
foreach (string file in Directory.GetFiles(textBox1.Text))//获取指定目录下的文件名称,不包括文件夹
{
string OldStr = Path.GetDirectoryName(file) + @"\" + Path.GetFileName(file);
string NewStr = Path.GetDirectoryName(file) + @"\" + textBox2.Text + i.ToString() + Path.GetExtension(file);
//MessageBox.Show(Path.GetExtension(file));
//MessageBox.Show(NewStr);
FileInfo fi = new FileInfo(OldStr);
fi.MoveTo(NewStr);//将制定文件移动到新位置,并提供新的文件名
i++;
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/Mr-xue/p/4607212.html