private async void listViewHistory_MouseDoubleClick(object sender, MouseEventArgs e) { //播放音乐 ListViewItem item = listViewHistory.GetItemAt(e.X, e.Y); selectedSong = songs[item.Index - 1]; string dirImgTemp = "..\\..\\temp\\img"; // 缓存的专辑所在文件夹 string dirLrcTemp = "..\\..\\temp\\lrc"; // 缓存的歌词所在文件夹 string imgFile = selectedSong.SongName + "-" + selectedSong.SongSinger + "-" + selectedSong.SongAlbum + ".jpg"; string lrcFile = selectedSong.SongName + "-" + selectedSong.SongSinger + "-" + selectedSong.SongAlbum + ".txt"; //播放音乐 play(selectedSong); if (!Directory.Exists(dirImgTemp)) //如果不存在dirImgTemp就创建 { Directory.CreateDirectory(dirImgTemp); } if (!Directory.Exists(dirLrcTemp)) //如果不存在dirLrcTemp就创建 { Directory.CreateDirectory(dirLrcTemp); } await Task.Run(() => { if (!File.Exists(dirImgTemp + "\\" + imgFile)) //如果不存在,则获取URL下载 { if (!(DownLoader.DownloadSongOrImg(selectedSong.ImgDownloadURL, dirImgTemp + "\\" + imgFile))) { //错误处理 } } if (!File.Exists(dirLrcTemp + "\\" + lrcFile)) //如果不存在,则获取URL下载 { if (!(DownLoader.DownloadLrc(selectedSong.LrcDownloadURL, dirLrcTemp + "\\" + lrcFile))) { //错误处理 } } }); IncreaseHistory(selectedSong); pictureBoxAlbum.BackgroundImage = Image.FromFile(dirImgTemp + "\\" + imgFile); labelSongInfo.Text = selectedSong.SongName + "- " + selectedSong.SongSinger; historyPaint(); }
private async void toolStripMenuItem2_Click(object sender, EventArgs e) { string dirSongDownload = "..\\..\\download\\music"; // 下载的音乐所在文件夹 string dirImgDownload = "..\\..\\download\\img"; // 下载的专辑图片所在文件夹 string dirLrcDownload = "..\\..\\download\\lrc"; // 下载的歌词所在文件夹 string dirImgTemp = "..\\..\\temp\\img"; // 缓存的专辑所在文件夹 string dirLrcTemp = "..\\..\\temp\\lrc"; // 缓存的歌词所在文件夹 string songFile = selectedSong.SongName + "-" + selectedSong.SongSinger + "-" + selectedSong.SongAlbum + ".mp3"; string imgFile = selectedSong.SongName + "-" + selectedSong.SongSinger + "-" + selectedSong.SongAlbum + ".jpg"; string lrcFile = selectedSong.SongName + "-" + selectedSong.SongSinger + "-" + selectedSong.SongAlbum + ".txt"; if (!Directory.Exists(dirSongDownload)) //如果不存在dirSongDownload就创建 { Directory.CreateDirectory(dirSongDownload); } if (!Directory.Exists(dirImgDownload)) //如果不存在dirImgDownload就创建 { Directory.CreateDirectory(dirImgDownload); } if (!Directory.Exists(dirLrcDownload)) //如果不存在dirLrcDownload就创建 { Directory.CreateDirectory(dirLrcDownload); } await Task.Run(() => { if (!(DownLoader.DownloadSongOrImg(selectedSong.SongDownloadURL, dirSongDownload + "\\" + songFile))) { //错误处理 } if (File.Exists(dirImgTemp + "\\" + imgFile)) //如果存在缓存,直接复制 { if (!File.Exists(dirImgDownload + "\\" + imgFile)) { FileInfo file = new FileInfo(dirImgTemp + "\\" + imgFile); file.CopyTo(dirImgDownload + "\\" + imgFile); } } else //如果不存在,则获取URL下载 { if (!(DownLoader.DownloadSongOrImg(selectedSong.ImgDownloadURL, dirImgDownload + "\\" + imgFile))) { //错误处理 } } if (File.Exists(dirLrcTemp + "\\" + lrcFile)) //如果存在缓存,直接复制 { if (!File.Exists(dirLrcDownload + "\\" + lrcFile)) { FileInfo file = new FileInfo(dirLrcTemp + "\\" + lrcFile); file.CopyTo(dirLrcDownload + "\\" + lrcFile); } } else //如果不存在,则获取URL下载 { if (!(DownLoader.DownloadLrc(selectedSong.LrcDownloadURL, dirLrcDownload + "\\" + lrcFile))) { //错误处理 } } SQLiteConnection conn = new SQLiteConnection("data source=music_player.db"); conn.Open(); SQLiteCommand cmd = new SQLiteCommand($"SELECT * FROM local WHERE songID = '{songFile}'", conn); SQLiteDataReader reader = cmd.ExecuteReader(); if (!reader.HasRows) { cmd = new SQLiteCommand($"INSERT INTO local VALUES('{selectedSong.SongName}', '{selectedSong.SongSinger}', '{selectedSong.SongAlbum}', '{songFile}', '{imgFile}', '{lrcFile}',1)", conn); cmd.ExecuteNonQuery(); } reader.Close(); conn.Close(); }); }