如何读取MP3歌曲文件标志信息

来源:互联网 发布:mac终端怎么进服务器 编辑:程序博客网 时间:2024/04/28 00:07
此编程方法主要是以字节方式读取mp3歌曲文件的名称,艺术家以及发行日期等文件标志信息,这些标志信息位于mp3文件的结束位置,并以"TAG"开始,共128字节。主要源代码如下:
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {//判断目录是否已经存在
            String MyDir1 = @"C:/WINDOWS/SYSTEM";
            String MyDir2 = @"C:/WINDOW/SYSTEM";
            String MyInfo = "";
            if (Directory.Exists(MyDir1))
                MyInfo+=MyDir1+" 目录已经存在!/n" ;
            else
                MyInfo += MyDir1 + " 目录已经不存在!/n";
            if (Directory.Exists(MyDir2))
                MyInfo += MyDir2 + " 目录已经存在!/n";
            else
                MyInfo += MyDir2 + " 目录已经不存在!/n";
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button2_Click(object sender, EventArgs e)
        {//判断文件是否已经存在
            String MyFile1 = @"C:/boot.ini";
            String MyFile2 = @"D:/boot.ini";
            String MyInfo = "";
            if (File.Exists(MyFile1))
                MyInfo += MyFile1 + " 文件已经存在!/n";
            else
                MyInfo += MyFile1 + " 文件已经不存在!/n";
            if (File.Exists(MyFile2))
                MyInfo += MyFile2 + " 文件已经存在!/n";
            else
                MyInfo += MyFile2 + " 文件已经不存在!/n";
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button3_Click(object sender, EventArgs e)
        {//获取当前工作目录
            String MyPath = "当前工作目录是:";
            MyPath+=Directory.GetCurrentDirectory();
            MessageBox.Show(MyPath, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);          
        }
        private void button4_Click(object sender, EventArgs e)
        {//设置当前工作目录
            String MyInfo= "设置当前工作目录为:";
            String MyPath = "C://Windows";
            Directory.SetCurrentDirectory(MyPath);
            MyInfo += MyPath + " 操作成功!";
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                
        }

        private void button5_Click(object sender, EventArgs e)
        {//创建并读写临时文件
            string MyTempFile = Path.GetTempFileName();
            //向临时文件写入信息
            FileStream MyStream = new FileStream(MyTempFile, FileMode.Open);
            StreamWriter MyWriter = new StreamWriter(MyStream, Encoding.UTF8);
            MyWriter.WriteLine(MyTempFile+" 临时文件的内容如下:");
            foreach (string MyFileName in Directory.GetFiles("C://"))
                MyWriter.WriteLine(MyFileName);
            MyWriter.Flush();
            MyWriter.Close();
            MyStream.Close();
            //从临时文件读取信息
            StreamReader MyReader = new StreamReader(MyTempFile, Encoding.UTF8);
            string MyInfo = MyReader.ReadToEnd();
            MyReader.Close();
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //删除临时文件
            File.Delete(MyTempFile);
        }

        private void button6_Click(object sender, EventArgs e)
        {//管理独立存储文件
            IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForAssembly();
            //在独立存储中创建一个文件夹
            MyStore.CreateDirectory("MyFolder");
            //创建独立存储文件
            Stream MyStream = new IsolatedStorageFileStream("MyFile3.txt", FileMode.Create, MyStore);
            //Stream MyStream = new IsolatedStorageFileStream("MyFile2.txt", FileMode.Create, MyStore);
            //Stream MyStream = new IsolatedStorageFileStream("MyFile3.txt", FileMode.Create, MyStore);
            //Stream MyStream = new IsolatedStorageFileStream("MyFile4.txt", FileMode.Create, MyStore);
            //Stream MyStream = new IsolatedStorageFileStream("MyFile5.txt", FileMode.Create, MyStore);
            StreamWriter MyWriter = new StreamWriter(MyStream, Encoding.UTF8);
            MyWriter.WriteLine("C://文件夹中的文件包括:");
            foreach (string MyFileName in Directory.GetFiles("C://"))
                MyWriter.WriteLine(MyFileName);
            MyWriter.Flush();
            MyWriter.Close();
            MyStream.Close();
            String MyInfo = "";
            //独立存储区中的信息
            MyInfo+="当前尺寸: " + MyStore.CurrentSize.ToString()+"/n";
            MyInfo+="存储区范围: " + MyStore.Scope.ToString()+"/n";
            MyInfo+="包含的文件有:/n";
            string[] MyFiles = MyStore.GetFileNames("*.*");
            foreach (string MyFile in MyFiles)
            {
                MyInfo+=MyFile+"/n";
            }
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button7_Click(object sender, EventArgs e)
        {//监视文件创建删除操作
            //配置文件系统监视器
            FileSystemWatcher MyWatch = new FileSystemWatcher();
            MyWatch.Path = Application.StartupPath;
            MyWatch.Filter = "*.*";
            MyWatch.IncludeSubdirectories = true;
            //添加文件系统监视器事件句柄
            MyWatch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
            MyWatch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
            MyWatch.EnableRaisingEvents = true;
            if (File.Exists("MyTestFile.txt"))
            {
                File.Delete("MyTestFile.txt");
            }
            FileStream MyStream = new FileStream("MyTestFile.txt", FileMode.Create);
            MyStream.Close();
        }
        private static void OnCreatedOrDeleted(object sender, FileSystemEventArgs e)
        {//当在当前目录中创建和删除文件时显示提示信息
            String MyInfo =  e.FullPath +"文件已经被成功";
            switch (e.ChangeType)
            {
                case WatcherChangeTypes.Created:
                    MyInfo+="创建!";
                    break;
                case WatcherChangeTypes.Deleted:
                    MyInfo += "删除!";
                    break;
            }
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button8_Click(object sender, EventArgs e)
        {//加密帐户依赖文件,然后试着在另一个帐户下打开该文件
            String MyFile = "MyText.txt";
            File.Encrypt(MyFile);
        }
        private void button9_Click(object sender, EventArgs e)
        {//解密帐户依赖文件
            String MyFile = "MyText.txt";
            File.Decrypt(MyFile);
        }

        private void button10_Click(object sender, EventArgs e)
        {//获取启动程序的执行文件路径
            string MyInfo = "启动了应用程序的可执行文件的路径是:/n" + Application.ExecutablePath;
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);      
        }

        private void button11_Click(object sender, EventArgs e)
        {//获取启动程序的执行文件目录
            string MyInfo = "启动了应用程序的可执行文件的目录是:/n" + Application.StartupPath;
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);          
        }

        private void button12_Click(object sender, EventArgs e)
        {//读取MP3歌曲文件标志信息
            string MyFileName = "C://after.mp3";
            FileStream  MyStream=new FileStream(MyFileName, FileMode.Open);
            MyStream.Seek(-128,SeekOrigin.End);
            Byte[] MyTag=new Byte[3];
            MyStream.Read(MyTag,0,3);
            string MyInfo=MyFileName +"歌曲信息如下:";
            if (System.Text.Encoding.Default.GetString(MyTag) == "TAG")
            {
                MyInfo += "/n歌名:" + GetTagData(MyStream, 30);
                MyInfo += "/n艺术家:" + GetTagData(MyStream, 30);
                MyInfo += "/n签名册:" + GetTagData(MyStream, 30);
                MyInfo += "/n发行日期:" + GetTagData(MyStream, 4);
            }
            MyStream.Close();
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private string GetTagData(Stream MyStream,int MyLength)
        {
            Byte[] MyBytes=new Byte[MyLength];
            MyStream.Read(MyBytes,0,MyLength);
            string MyTagData = System.Text.Encoding.Default.GetString(MyBytes);
            char[] MyTrimChars ={char.Parse(" ") };
            MyTagData = MyTagData.Trim(MyTrimChars);
            return MyTagData ;
        }

        private void button13_Click(object sender, EventArgs e)
        {//指定文件保存的编码方式
            FileStream MyStream = new FileStream("MyUTF8UTF16ASCII.bin", FileMode.Create);
            StreamWriter MyUTF8 = new StreamWriter(MyStream, Encoding.UTF8);
            MyUTF8.Write("This is in UTF8");
            MyUTF8.Flush();
            StreamWriter MyUTF16 = new StreamWriter(MyStream, Encoding.Unicode);
            MyUTF16.Write("This is in UTF16");
            MyUTF16.Flush();
            StreamWriter MyASCII = new StreamWriter(MyStream, Encoding.ASCII);
            MyASCII.Write("This is in ASCII");
            MyASCII.Flush();
            MyASCII.Close();
        }

        private void button14_Click(object sender, EventArgs e)
        {//读取WAV歌曲文件标志信息
            string MyFile = Application.StartupPath + "//Windows XP 启动.wav";
            FileInfo MyFileInfo = new FileInfo(MyFile);
            System.IO.FileStream MyStream=MyFileInfo.OpenRead();
            string MyInfo=MyFile+"文件的标志信息如下:";
            if (MyStream.Length >= 44)
            {
                byte[] MyBytes=new byte[44];
                MyStream.Read(MyBytes,0,44); 
                System.Text.Encoding.Default.GetString(MyBytes,0,4);

                if (System.Text.Encoding.Default.GetString(MyBytes, 0, 4) == "RIFF" && System.Text.Encoding.Default.GetString(MyBytes, 8, 4) == "WAVE" && System.Text.Encoding.Default.GetString(MyBytes, 12, 4) == "fmt ")
                {
                    MyInfo+="/n GroupID:"+System.Text.Encoding.Default.GetString(MyBytes, 0, 4) as string ;
                    System.BitConverter.ToInt32(MyBytes, 4);
                    MyInfo+="/n FileSize:"+ System.BitConverter.ToInt32(MyBytes, 4) as string ;
                    MyInfo+="/n RiffType:"+System.Text.Encoding.Default.GetString(MyBytes, 8, 4) as string ;
                    MyInfo+="/n ChunkID:"+System.Text.Encoding.Default.GetString(MyBytes, 12, 4) as string ;
                    MyInfo+="/n ChunkSize:"+System.BitConverter.ToInt32(MyBytes, 16) as string ;
                    MyInfo+="/n WFormatTag:"+System.BitConverter.ToInt16(MyBytes, 20) as string ;
                    MyInfo+="/n WChannels:"+System.BitConverter.ToUInt16(MyBytes, 22) as string ;
                    MyInfo+="/n DWSamplesPerSec:"+System.BitConverter.ToUInt32(MyBytes, 24) as string ;
                    MyInfo+="/n DWavBytesPerSec:"+System.BitConverter.ToUInt32(MyBytes, 28) as string ;
                    MyInfo+="/n WBLockAlign:"+System.BitConverter.ToUInt16(MyBytes, 32) as string ;
                    MyInfo+="/n WBitsPerSample:"+System.BitConverter.ToUInt16(MyBytes, 34) as string ;
                    MyInfo+="/n DataChunkID:"+System.Text.Encoding.Default.GetString(MyBytes, 36, 4) as string ;
                    MyInfo+="/n DataSize:"+System.BitConverter.ToInt32(MyBytes, 40) as string ;
                }
            }
            MyStream.Close();
            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }