代码改变世界

分享一个管理硬盘中的音乐文件的ASP.NET程序,仅限于我个人的处理方式

2011-04-25 11:42  音乐让我说  阅读(290)  评论(0编辑  收藏  举报

我的硬盘中有很多文件名相同的音乐文件,分布在不同的目录,于是自己写了一个小程序来处理这些文件!

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CopyNotExistMP3.aspx.cs" Inherits="WebUI.CopyNotExistMP3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnCopy" runat="server" OnClick="btnCopy_Click" Text="复制“最新”文件夹下的文件" />   
        <asp:Button ID="btnCopyKugou" runat="server" OnClick="btnCopyKugou_Click" Text="复制“Kugou”根文件夹下的文件" />   
        <asp:Button ID="btnFindSameNameFile" runat="server" OnClick="btnFindSameNameFile_Click" Text="找出同名文件" />
        <asp:Button ID="btnUploaded" runat="server" OnClick="btnUploaded_Click" Text="已经上传了的图片" />
    </div>
    <div>
        <asp:Literal ID="ltMessage" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace WebUI
{
    public partial class CopyNotExistMP3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            CopyFile("D:\\Kugou\\最新", "E:\\第一次备份\\Kugou\\最新");
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="bigMp3FolderPath"></param>
        /// <param name="smallMp3FolderPath"></param>
        protected void CopyFile(string bigMp3FolderPath, string smallMp3FolderPath)
        {
            string destinationPath = "E:\\NewKugou";
            if (Directory.Exists(bigMp3FolderPath) && Directory.Exists(smallMp3FolderPath))
            {
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                DirectoryInfo bigMp3FolderInfo = new DirectoryInfo(bigMp3FolderPath);
                FileInfo[] bigMp3Files = bigMp3FolderInfo.GetFiles();
                foreach (FileInfo item in bigMp3Files)
                {
                    if (!File.Exists(smallMp3FolderPath + "\\" + item.Name))
                    {
                        File.Copy(item.FullName, destinationPath + "\\" + item.Name, true);
                    }
                }
                this.ltMessage.Text = "文件复制完成!" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.mmm");
            }
            else
            {
                this.ltMessage.Text = "路径不存在!";
            }
        }

        protected void btnCopyKugou_Click(object sender, EventArgs e)
        {
            CopyFile("D:\\Kugou", "E:\\第一次备份\\Kugou");
        }

        protected void btnFindSameNameFile_Click(object sender, EventArgs e)
        {
            string directoryFullName = "D:\\我的音乐";
            StringBuilder builder = new StringBuilder();
            Dictionary<string, string> sameFilesDic = new Dictionary<string, string>();
            if (Directory.Exists(directoryFullName))
            {
                DirectoryInfo bigMp3FolderInfo = new DirectoryInfo(directoryFullName);
                FileInfo[] allMp3Files = bigMp3FolderInfo.GetFiles("*.*", SearchOption.AllDirectories);
                foreach (FileInfo item in allMp3Files)
                {
                    foreach (FileInfo itemChild in allMp3Files)
                    {
                        if (item.Name == itemChild.Name && item.FullName != itemChild.FullName)
                        {
                            if (!sameFilesDic.Keys.Contains(item.FullName) && !sameFilesDic.Keys.Contains(itemChild.FullName))
                            {
                                sameFilesDic.Add(item.FullName, itemChild.FullName);
                                builder.AppendFormat("{0}    与    {1}<br/>", item.FullName, itemChild.FullName);
                            }
                        }
                    }
                }
            }
            else
            {
                this.ltMessage.Text = "路径不存在!";
            }
            this.ltMessage.Text = builder.ToString();
        }

        /// <summary>
        /// 找出已经上传了的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUploaded_Click(object sender, EventArgs e)
        {
            string directoryFullName = "E:\\复件 青春的岁月";
            StringBuilder builder = new StringBuilder();
            if (Directory.Exists(directoryFullName))
            {
                DirectoryInfo bigMp3FolderInfo = new DirectoryInfo(directoryFullName);
                FileInfo[] allMp3Files = bigMp3FolderInfo.GetFiles();
                builder.AppendFormat("共{0}个文件<br/>", allMp3Files.Length);
                foreach (FileInfo item in allMp3Files)
                {
                    builder.AppendFormat("{0}<br/>", item.Name);
                }
            }
            else
            {
                this.ltMessage.Text = "路径不存在!";
            }
            this.ltMessage.Text = builder.ToString();
        }
    }
}

谢谢浏览!