using System; using System.IO; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Shared; //using HDL_ON.UI; using HDL_ON.DAL.Net; using HDL_ON.DAL; using HDL_ON.Entity; namespace HDL_ON { public static class FileUtils { public static string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; /// /// 创建一个住宅的备份文件夹 /// public static string CreateRegionBackup(string regionId) { var path = Path.Combine(RootPath, regionId); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } return path + "/"; } /// /// 判断是否已经存在住宅的本地文件夹 /// public static bool ExistRegion(string regionId) { var path = Path.Combine(RootPath, regionId); return Directory.Exists(path); } /// /// 删除指定区域里面的所有文件 /// /// Path. public static void DeleteRegionFiles(string path) { foreach (var fileName in ReadRegionFiles(path)) { File.Delete(Path.Combine(path, fileName)); MainPage.Log("Del region file :" + fileName); } } /// /// 读取区域文件夹里面的文件 /// static List ReadRegionFiles(string regionRootPath) { List listFiles = new List(); DirectoryInfo TheFolder = new DirectoryInfo(regionRootPath); foreach (FileInfo NextFile in TheFolder.GetFiles()) { listFiles.Add(NextFile.Name); } return listFiles; } /// /// 恢复区域文件夹里面的文件 /// public static void RestoreRegionFiles(string regionRootPath) { DirectoryInfo TheFolder = new DirectoryInfo(regionRootPath); int index = 0; var thFolders = TheFolder.GetFiles(); int folderCount = thFolders.Length; foreach (FileInfo NextFile in thFolders) { index++; if (NextFile.Name == "headImage.png") { continue; } if (NextFile.Name == "null" || NextFile.Name == "UserConfig" || NextFile.Name == "Register_File" || NextFile.Name == "AccountListDB") { continue; } FileInfo fileInfo = new FileInfo(regionRootPath + NextFile.Name); if (fileInfo.Exists) { fileInfo.MoveTo(Application.RootPath + NextFile.Name); MainPage.Log("Restroe : " + NextFile.Name); } } } /// /// 遍历文件 /// public static List ReadFiles() { List listFiles = new List(); DirectoryInfo TheFolder = new DirectoryInfo(RootPath); foreach (FileInfo NextFile in TheFolder.GetFiles()) { listFiles.Add(NextFile.Name); } return listFiles; } /// /// 更改文件名 /// /// Old file path. /// New file path. public static void ReNameFile(string oldFilePath, string newFilePath) { if (oldFilePath == newFilePath) { return; } DeleteFile(newFilePath); FileInfo fileInfo = new FileInfo(Application.RootPath + oldFilePath); if (fileInfo.Exists) { fileInfo.MoveTo(Application.RootPath + newFilePath); } } public static void ReNameFoler(string srcFolderPath, string destFolderPath) { if (srcFolderPath == destFolderPath) { return; } var srcPath = Path.Combine(RootPath, srcFolderPath); #if DEBUG DirectoryInfo root = new DirectoryInfo (RootPath); foreach (DirectoryInfo d in root.GetDirectories ()) { MainPage.Log ("文件夹:" + d.Name); } #endif if (Directory.Exists(srcPath)) { DirectoryInfo folder = new DirectoryInfo(srcPath); var path = Path.Combine(RootPath, destFolderPath); if (Directory.Exists(path)) { FileAttributes attr0 = File.GetAttributes(path); if (attr0 == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } } folder.MoveTo(path); #if DEBUG List listFiles = new List (); DirectoryInfo TheFolder = new DirectoryInfo (path); foreach (FileInfo NextFile in TheFolder.GetFiles ()) { MainPage.Log (NextFile.Name); } #endif } #if DEBUG foreach (DirectoryInfo d in root.GetDirectories ()) { MainPage.Log ("文件夹:" + d.Name); } #endif } /// /// 文件是否存在 /// /// File path. public static bool Exists(string filePath) { //System.MainPage.WriteLog ("Exists==" + Path.Combine (RootPath, filePath)); return File.Exists(Path.Combine(RootPath, filePath)); } /// /// Deletes the file. /// /// File name. public static void DeleteFile(string fileName) { if (fileName == null || fileName == "Language.ini" || fileName == "AccountListDB") { return; } File.Delete(Path.Combine(RootPath, fileName)); MainPage.Log("删除文件名为:" + fileName); } /// /// Deletes all file. /// public static void DeleteAllFile() { List filesList = ReadFiles(); for (int j = 0; j < filesList.Count; j++) { var f = filesList[j]; if (f == "linphonerc" || f == "AccountListDB" || f == "Register_File") continue; DeleteFile(f); MainPage.Log($"Del file :{f}"); } } /// /// Writes the file by bytes. /// /// true, if file by bytes was writed, false otherwise. /// File name. /// Bytes. public static bool WriteFileByBytes(string fileName, byte[] bytes) { if (fileName == null || (fileName = fileName.Trim()) == ("")) { return false; } FileStream fs = null; try { fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); fs.Write(bytes, 0, bytes.Length); fs.Flush(); MainPage.Log("SaveFile:" + fileName); return true; } catch (Exception ex) { MainPage.Log("FileUtiles Code 113:" + ex.ToString()); return false; } finally { try { if (fs != null) { fs.Close(); } } catch (Exception ex) { MainPage.Log("FileUtils Code 121 :" + ex.ToString()); } } } /// /// Writes the file by input stream. /// /// true, if file by input stream was writed, false otherwise. /// File name. /// Input stream. /// If set to true is need root path. public static bool WriteFileByInputStream(string fileName, Stream inputStream) { if (fileName == null || (fileName = fileName.Trim()) == ("")) { return false; } FileStream fs = null; try { byte[] buffer = new byte[4 * 1024]; int length = 0; while ((length = inputStream.Read(buffer, 0, buffer.Length)) != 0) { if (fs == null) { fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); } fs.Write(buffer, 0, length); fs.Flush(); } return true; } catch { return false; } finally { try { if (fs != null) { fs.Close(); } } catch { } } } /// /// read file /// /// The file. /// File name. public static byte[] ReadImage(string fileName) { if (Shared.Application.IsPad) { fileName = "Pad/" + fileName; } else { fileName = "Phone/" + fileName; } FileStream fs = null; try { if (File.Exists(Path.Combine(RootPath, fileName))) { fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); } else if (File.Exists(fileName)) { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); } else { return new byte[0]; } byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); return bytes; } catch { return new byte[0]; } finally { try { if (fs != null) { fs.Close(); } } catch { } } } public static byte[] ReadFile(string fileName) { FileStream fs = null; try { if (File.Exists(Path.Combine(RootPath, fileName))) { fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); } else if (File.Exists(fileName)) { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); } else { return new byte[0]; } byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); return bytes; } catch { return new byte[0]; } finally { try { if (fs != null) { fs.Close(); } } catch { } } } /// /// 下载电台、音乐、DLNA等等网络图片 /// /// /// public static void DownLoadImage(string fileName, string url) { #region 如果当前目录不存在,就初始化 string directioryPath = null; if (Shared.Application.IsPad) { directioryPath = "Pad/" + "MusicImage"; } else { directioryPath = "Phone/" + "MusicImage"; } directioryPath = Path.Combine(RootPath, directioryPath); if (!Directory.Exists(directioryPath)) { Directory.CreateDirectory(directioryPath); } #endregion if (!File.Exists(fileName)) { System.Threading.Tasks.Task.Run(() => { FileStream fs = null; try { HDL_ON.DAL.Net.MyWebClient webClient = new HDL_ON.DAL.Net.MyWebClient(); byte[] recevieBytes = webClient.DownloadData(new Uri(url)); fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); fs.Write(recevieBytes, 0, recevieBytes.Length); fs.Flush(); } catch { } finally { if (fs != null) { fs.Close(); } } }); } } /// /// 下载电台、音乐、DLNA等等网络图片 /// /// /// public static string DownLoadImageFormBaidu(string album, string artist) { var url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=" + artist; string directioryPath = "MusicImage"; directioryPath = Path.Combine(RootPath, directioryPath); if (!Directory.Exists(directioryPath)) { Directory.CreateDirectory(directioryPath); } try { var song = Newtonsoft.Json.JsonConvert.DeserializeObject(new HDL_ON.DAL.Net.MyWebClient().DownloadString(new Uri(url))); if (song == null) { return null; } foreach (var tempAlbum in song.album) { if (tempAlbum.albumname == album && artist == tempAlbum.artistname) { var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempAlbum.artistpic.Split('@')[0] + "@s_1,w_160,h_160")); var filePath = Path.Combine(directioryPath, "AlbumArtistImage_" + album); var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); fs.Write(bytes, 0, bytes.Length); fs.Flush(); fs.Close(); return filePath; } } foreach (var tempArtist in song.artist) { if (artist == tempArtist.artistname) { var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempArtist.artistpic.Split('@')[0] + "@s_1,w_160,h_160")); var filePath = Path.Combine(directioryPath, "AlbumArtistImage_" + artist); var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); fs.Write(bytes, 0, bytes.Length); fs.Flush(); fs.Close(); return filePath; } } } catch { } return null; } } [System.Serializable] class Song { //string [] ss = new string [] { }; public List album = new List(); public List artist = new List(); public string sss; } [System.Serializable] class Album { public string albumname; public string artistname; public string artistpic; } [System.Serializable] class Artist { public string artistname; public string artistpic; } }