自己写的一个整理硬盘文件的小程序
简单些了一下
还有很多错误没改正
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Fileclass
{
publicpartialclass Form1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid btnclass_Click(object sender, EventArgs e)
{
if(openFilepath.FileName!="")
{
//先检查是否存在文件夹没有则新建文件夹!
checkfile();
// 获得文件夹文件某目录下的所有文件(包括子目录下文件)并移动文件
getfile(path);
MessageBox.Show("文件移动完毕!");
writelog("文件移动完毕记录", "文件移动完毕!", "i");
}
}
privatevoid btnopen_Click(object sender, EventArgs e)
{
//设置默认打开路径
openFilepath.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
if (openFilepath.ShowDialog() == DialogResult.OK)
{
tbfilepath.Text = openFilepath.FileName;
}
}
privatestaticstring path ="";
Dictionary<string, string> dict =new Dictionary<string, string>();
//读取格式文本
privatestring formats()
{
string formats =null;
using (StreamReader sr =new StreamReader(AppDomain.CurrentDomain.BaseDirectory+"//formats.txt", System.Text.Encoding.Default))
{
formats = sr.ReadToEnd();
}
return formats;
}
//先检查是否存在文件夹没有则新建文件夹!
privatevoid checkfile()
{
string filepath = tbfilepath.Text;
if (filepath !="")
{
//获得当前选择文件的文件夹
string format = formats();
string[] f = format.Split('/');
string fpath = filepath.Substring(0, filepath.LastIndexOf('\\'));
path = fpath;
string spath ="";
if (tbcrepath.Text =="")
{
spath = fpath +"\\Sortfile";
}
else
{
spath = fpath +"\\"+ tbcrepath.Text;
}
//判断文件夹是否存在
if (!Directory.Exists(spath))
{
// 创建目录\Sortfile
DirectoryInfo dir = Directory.CreateDirectory(spath);
foreach (string s in f)
{
string crepath = spath +"\\"+ s;
//创建文本内的格式文件夹
if (!Directory.Exists(crepath))
{
DirectoryInfo d = Directory.CreateDirectory(crepath);
//讲读取的文本内格式放入dict
dict.Add(s, crepath);
writelog("创建文件夹", crepath, "i");
}
}
}
else
{
foreach (string s in f)
{
//还未调试成功
writelog("创建文件夹", "文件夹已经存在", "i");
dict.Add(s, spath +"\\"+ s);
}
}
}
}
// 获得文件夹文件某目录下的所有文件(包括子目录下文件)
privatevoid getfile(string srcPath)
{
try
{
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)
if (System.IO.Directory.Exists(file))
{
if (file == path)
{
continue;
}
else
{
getfile(file);
}
}
else
{
writelog("搜索到文件", file, "i");
try
{
//移动到之前创建的格式文件夹内
movefile(file, dict[getformat(file)] +"\\"+ getfilename(file));
}
catch (Exception e)
{
writelog("移动此文件出错", file, "e");
writelog("出错信息", e.ToString(), "e");
}
}
}
}
catch (Exception e)
{
writelog("搜索出错", e.ToString(), "e");
}
}
//移动文件
privatevoid movefile(string oldpath, string newpath)
{
//获取文件路径
string filepath = newpath.Substring(0, newpath.LastIndexOf('.'));
//获取文件格式
string fileformat ="."+ getformat(newpath);
if (File.Exists(filepath + fileformat))
{
//定义一个FileInfo对象,使之与filePath所指向的文件向关联,
//以获取其大小
//FileInfo fileInfo = new FileInfo(oldpath);
//if (fileInfo.Length > 0)
//{
//如果文件名相同把文件名加_1后移动
File.Move(oldpath, filepath +"_"+ Guid.NewGuid().ToString() + fileformat);
writelog("文件已重命名", oldpath, "i");
//}
}
else
{
File.Move(oldpath, filepath + fileformat);
//File.Move(oldpath, newpath + getfilename(oldpath));
writelog("成功移动文件", oldpath, "i");
}
}
//获取文件格式
privatestring getformat(string path)
{
//获取文件格式
string fileformat = path.Substring(path.LastIndexOf('.') +1);
return fileformat;
}
//获取文件名字(带后缀名)
privatestring getfilename(string path)
{
//获取文件格式
string filename = path.Substring(path.LastIndexOf('\\') +1);
return filename;
}
//将操作的文件记录到日志
privatevoid writelog(string names, string infom, string flag)
{
log4net.Config.XmlConfigurator.Configure();//把AppConfig中的配置信息读进来
log4net.ILog log = log4net.LogManager.GetLogger(names);
if (flag =="e")
{
log.Error(infom);
}
if (flag =="i")
{
log.Info(infom);
}
if (flag =="d")
{
log.Debug(infom);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Fileclass
{
publicpartialclass Form1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid btnclass_Click(object sender, EventArgs e)
{
if(openFilepath.FileName!="")
{
//先检查是否存在文件夹没有则新建文件夹!
checkfile();
// 获得文件夹文件某目录下的所有文件(包括子目录下文件)并移动文件
getfile(path);
MessageBox.Show("文件移动完毕!");
writelog("文件移动完毕记录", "文件移动完毕!", "i");
}
}
privatevoid btnopen_Click(object sender, EventArgs e)
{
//设置默认打开路径
openFilepath.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
if (openFilepath.ShowDialog() == DialogResult.OK)
{
tbfilepath.Text = openFilepath.FileName;
}
}
privatestaticstring path ="";
Dictionary<string, string> dict =new Dictionary<string, string>();
//读取格式文本
privatestring formats()
{
string formats =null;
using (StreamReader sr =new StreamReader(AppDomain.CurrentDomain.BaseDirectory+"//formats.txt", System.Text.Encoding.Default))
{
formats = sr.ReadToEnd();
}
return formats;
}
//先检查是否存在文件夹没有则新建文件夹!
privatevoid checkfile()
{
string filepath = tbfilepath.Text;
if (filepath !="")
{
//获得当前选择文件的文件夹
string format = formats();
string[] f = format.Split('/');
string fpath = filepath.Substring(0, filepath.LastIndexOf('\\'));
path = fpath;
string spath ="";
if (tbcrepath.Text =="")
{
spath = fpath +"\\Sortfile";
}
else
{
spath = fpath +"\\"+ tbcrepath.Text;
}
//判断文件夹是否存在
if (!Directory.Exists(spath))
{
// 创建目录\Sortfile
DirectoryInfo dir = Directory.CreateDirectory(spath);
foreach (string s in f)
{
string crepath = spath +"\\"+ s;
//创建文本内的格式文件夹
if (!Directory.Exists(crepath))
{
DirectoryInfo d = Directory.CreateDirectory(crepath);
//讲读取的文本内格式放入dict
dict.Add(s, crepath);
writelog("创建文件夹", crepath, "i");
}
}
}
else
{
foreach (string s in f)
{
//还未调试成功
writelog("创建文件夹", "文件夹已经存在", "i");
dict.Add(s, spath +"\\"+ s);
}
}
}
}
// 获得文件夹文件某目录下的所有文件(包括子目录下文件)
privatevoid getfile(string srcPath)
{
try
{
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)
if (System.IO.Directory.Exists(file))
{
if (file == path)
{
continue;
}
else
{
getfile(file);
}
}
else
{
writelog("搜索到文件", file, "i");
try
{
//移动到之前创建的格式文件夹内
movefile(file, dict[getformat(file)] +"\\"+ getfilename(file));
}
catch (Exception e)
{
writelog("移动此文件出错", file, "e");
writelog("出错信息", e.ToString(), "e");
}
}
}
}
catch (Exception e)
{
writelog("搜索出错", e.ToString(), "e");
}
}
//移动文件
privatevoid movefile(string oldpath, string newpath)
{
//获取文件路径
string filepath = newpath.Substring(0, newpath.LastIndexOf('.'));
//获取文件格式
string fileformat ="."+ getformat(newpath);
if (File.Exists(filepath + fileformat))
{
//定义一个FileInfo对象,使之与filePath所指向的文件向关联,
//以获取其大小
//FileInfo fileInfo = new FileInfo(oldpath);
//if (fileInfo.Length > 0)
//{
//如果文件名相同把文件名加_1后移动
File.Move(oldpath, filepath +"_"+ Guid.NewGuid().ToString() + fileformat);
writelog("文件已重命名", oldpath, "i");
//}
}
else
{
File.Move(oldpath, filepath + fileformat);
//File.Move(oldpath, newpath + getfilename(oldpath));
writelog("成功移动文件", oldpath, "i");
}
}
//获取文件格式
privatestring getformat(string path)
{
//获取文件格式
string fileformat = path.Substring(path.LastIndexOf('.') +1);
return fileformat;
}
//获取文件名字(带后缀名)
privatestring getfilename(string path)
{
//获取文件格式
string filename = path.Substring(path.LastIndexOf('\\') +1);
return filename;
}
//将操作的文件记录到日志
privatevoid writelog(string names, string infom, string flag)
{
log4net.Config.XmlConfigurator.Configure();//把AppConfig中的配置信息读进来
log4net.ILog log = log4net.LogManager.GetLogger(names);
if (flag =="e")
{
log.Error(infom);
}
if (flag =="i")
{
log.Info(infom);
}
if (flag =="d")
{
log.Debug(infom);
}
}
}
}