因为需要,编写了下面的小软件,对平常人没啥用,但思路还是有点价值的
功能是把一个目录里边的歌曲按照名字分类放入目录
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace filemanger
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox show;
private System.Windows.Forms.Button showfiles;
private System.Windows.Forms.Label number;
private System.Windows.Forms.Label l;
private System.Windows.Forms.Button move;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.show = new System.Windows.Forms.RichTextBox();
this.showfiles = new System.Windows.Forms.Button();
this.number = new System.Windows.Forms.Label();
this.l = new System.Windows.Forms.Label();
this.move = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// show
//
this.show.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.show.Location = new System.Drawing.Point(8, 8);
this.show.Name = "show";
this.show.ReadOnly = true;
this.show.Size = new System.Drawing.Size(432, 216);
this.show.TabIndex = 0;
this.show.Text = "电击开始显示文件信息";
//
// showfiles
//
this.showfiles.Location = new System.Drawing.Point(8, 232);
this.showfiles.Name = "showfiles";
this.showfiles.Size = new System.Drawing.Size(88, 23);
this.showfiles.TabIndex = 1;
this.showfiles.Text = "显示文件信息";
this.showfiles.Click += new System.EventHandler(this.showfiles_Click);
//
// number
//
this.number.ForeColor = System.Drawing.Color.Red;
this.number.Location = new System.Drawing.Point(328, 240);
this.number.Name = "number";
this.number.TabIndex = 2;
this.number.Click += new System.EventHandler(this.number_Click);
//
// l
//
this.l.Location = new System.Drawing.Point(208, 240);
this.l.Name = "l";
this.l.Size = new System.Drawing.Size(120, 23);
this.l.TabIndex = 3;
this.l.Text = "符合要求的mp3数目";
//
// move
//
this.move.Enabled = false;
this.move.Location = new System.Drawing.Point(120, 232);
this.move.Name = "move";
this.move.TabIndex = 4;
this.move.Text = "开始移动";
this.move.Click += new System.EventHandler(this.move_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 270);
this.Controls.Add(this.move);
this.Controls.Add(this.l);
this.Controls.Add(this.number);
this.Controls.Add(this.showfiles);
this.Controls.Add(this.show);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void showfiles_Click(object sender, System.EventArgs e)
{ show.Text="";
show.ReadOnly=true;
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);//获取程序目录
FileInfo[] mp3files = dir.GetFiles("*.mp3");
number.Text=mp3files.Length.ToString();
showfiles.Enabled=false;//下面输出目录下的mp3文件
foreach (FileInfo str in mp3files)
{
show.Text=show.Text+str+"\n";
}
move.Enabled=true;
}
private void number_Click(object sender, System.EventArgs e)
{
}
private void move_Click(object sender, System.EventArgs e)
{ string startpath;
int n;
n=0;
l.Text="已移动文件";
number.Text=n.ToString();
startpath=Application.StartupPath.ToString();
show.Text="";
show.ReadOnly=true;
DirectoryInfo dir = new DirectoryInfo(startpath);//注册类
FileInfo[] mp3files = dir.GetFiles("*.mp3");//获取所有的mp3文件
number.Text=mp3files.Length.ToString();//mp3文件总数
showfiles.Enabled=false;
foreach (FileInfo str in mp3files)//函数通过判断每个文件来分类
{ string files;
files=str.ToString().Substring(0,3)+"000";//获取文件名的前三个字符,然后加上三个0组成一个目录名
if(!Directory.Exists(startpath+"\\"+files))//判断前面的目录是否已经有了,如果没有再次生成目录
{
dir.CreateSubdirectory(files);
}
if(!File.Exists(startpath+"\\"+files+"\\"+str))//判断文件时候有了,如果没有就移动文件
{
str.MoveTo(startpath+"\\"+files+"\\"+str);
show.Text=show.Text+str+"---->"+files+"\n";
n=n+1;
number.Text=n.ToString();}
}
}
}
}
功能是把一个目录里边的歌曲按照名字分类放入目录
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace filemanger
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox show;
private System.Windows.Forms.Button showfiles;
private System.Windows.Forms.Label number;
private System.Windows.Forms.Label l;
private System.Windows.Forms.Button move;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.show = new System.Windows.Forms.RichTextBox();
this.showfiles = new System.Windows.Forms.Button();
this.number = new System.Windows.Forms.Label();
this.l = new System.Windows.Forms.Label();
this.move = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// show
//
this.show.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.show.Location = new System.Drawing.Point(8, 8);
this.show.Name = "show";
this.show.ReadOnly = true;
this.show.Size = new System.Drawing.Size(432, 216);
this.show.TabIndex = 0;
this.show.Text = "电击开始显示文件信息";
//
// showfiles
//
this.showfiles.Location = new System.Drawing.Point(8, 232);
this.showfiles.Name = "showfiles";
this.showfiles.Size = new System.Drawing.Size(88, 23);
this.showfiles.TabIndex = 1;
this.showfiles.Text = "显示文件信息";
this.showfiles.Click += new System.EventHandler(this.showfiles_Click);
//
// number
//
this.number.ForeColor = System.Drawing.Color.Red;
this.number.Location = new System.Drawing.Point(328, 240);
this.number.Name = "number";
this.number.TabIndex = 2;
this.number.Click += new System.EventHandler(this.number_Click);
//
// l
//
this.l.Location = new System.Drawing.Point(208, 240);
this.l.Name = "l";
this.l.Size = new System.Drawing.Size(120, 23);
this.l.TabIndex = 3;
this.l.Text = "符合要求的mp3数目";
//
// move
//
this.move.Enabled = false;
this.move.Location = new System.Drawing.Point(120, 232);
this.move.Name = "move";
this.move.TabIndex = 4;
this.move.Text = "开始移动";
this.move.Click += new System.EventHandler(this.move_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 270);
this.Controls.Add(this.move);
this.Controls.Add(this.l);
this.Controls.Add(this.number);
this.Controls.Add(this.showfiles);
this.Controls.Add(this.show);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void showfiles_Click(object sender, System.EventArgs e)
{ show.Text="";
show.ReadOnly=true;
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);//获取程序目录
FileInfo[] mp3files = dir.GetFiles("*.mp3");
number.Text=mp3files.Length.ToString();
showfiles.Enabled=false;//下面输出目录下的mp3文件
foreach (FileInfo str in mp3files)
{
show.Text=show.Text+str+"\n";
}
move.Enabled=true;
}
private void number_Click(object sender, System.EventArgs e)
{
}
private void move_Click(object sender, System.EventArgs e)
{ string startpath;
int n;
n=0;
l.Text="已移动文件";
number.Text=n.ToString();
startpath=Application.StartupPath.ToString();
show.Text="";
show.ReadOnly=true;
DirectoryInfo dir = new DirectoryInfo(startpath);//注册类
FileInfo[] mp3files = dir.GetFiles("*.mp3");//获取所有的mp3文件
number.Text=mp3files.Length.ToString();//mp3文件总数
showfiles.Enabled=false;
foreach (FileInfo str in mp3files)//函数通过判断每个文件来分类
{ string files;
files=str.ToString().Substring(0,3)+"000";//获取文件名的前三个字符,然后加上三个0组成一个目录名
if(!Directory.Exists(startpath+"\\"+files))//判断前面的目录是否已经有了,如果没有再次生成目录
{
dir.CreateSubdirectory(files);
}
if(!File.Exists(startpath+"\\"+files+"\\"+str))//判断文件时候有了,如果没有就移动文件
{
str.MoveTo(startpath+"\\"+files+"\\"+str);
show.Text=show.Text+str+"---->"+files+"\n";
n=n+1;
number.Text=n.ToString();}
}
}
}
}