Aween

向.NET进发

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
因为需要,编写了下面的小软件,对平常人没啥用,但思路还是有点价值的
功能是把一个目录里边的歌曲按照名字分类放入目录

 
   
 
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 窗体设计器生成的代码 

        
/// <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();}
 
                 
            }
 
        }
 
    }
 
}
posted on 2006-02-03 14:22  Aween's Blog  阅读(758)  评论(0编辑  收藏  举报