wxr
2020-09-01 7d005a7618e3d7a80d8ede3baf6ecc4bf8019cd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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) + "/";
 
        /// <summary>
        /// 创建一个住宅的备份文件夹
        /// </summary>
        public static string CreateRegionBackup(string regionId)
        {
            var path = Path.Combine(RootPath, regionId);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path + "/";
        }
        /// <summary>
        /// 判断是否已经存在住宅的本地文件夹
        /// </summary>
        public static bool ExistRegion(string regionId)
        {
            var path = Path.Combine(RootPath, regionId);
            return Directory.Exists(path);
        }
 
        /// <summary>
        /// 删除指定区域里面的所有文件
        /// </summary>
        /// <param name="path">Path.</param>
        public static void DeleteRegionFiles(string path)
        {
            foreach (var fileName in ReadRegionFiles(path))
            {
                File.Delete(Path.Combine(path, fileName));
                MainPage.Log("Del region file :" + fileName);
            }
        }
 
        /// <summary>
        /// 读取区域文件夹里面的文件
        /// </summary>
        static List<string> ReadRegionFiles(string regionRootPath)
        {
            List<string> listFiles = new List<string>();
            DirectoryInfo TheFolder = new DirectoryInfo(regionRootPath);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                listFiles.Add(NextFile.Name);
            }
            return listFiles;
        }
 
        /// <summary>
        /// 恢复区域文件夹里面的文件
        /// </summary>
        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);
                }
            }
        }
        
        /// <summary>
        /// 遍历文件
        /// </summary>
        public static List<string> ReadFiles()
        {
            List<string> listFiles = new List<string>();
            DirectoryInfo TheFolder = new DirectoryInfo(RootPath);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                listFiles.Add(NextFile.Name);
            }
            return listFiles;
        }
 
        /// <summary>
        /// 更改文件名
        /// </summary>
        /// <param name="oldFilePath">Old file path.</param>
        /// <param name="newFilePath">New file path.</param>
        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<string> listFiles = new List<string> ();
                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
        }
 
        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="filePath">File path.</param>
        public static bool Exists(string filePath)
        {
            //System.MainPage.WriteLog ("Exists==" + Path.Combine (RootPath, filePath));
            return File.Exists(Path.Combine(RootPath, filePath));
        }
 
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        public static void DeleteFile(string fileName)
        {
            if (fileName == null || fileName == "Language.ini" || fileName == "AccountListDB")
            {
                return;
            }
            File.Delete(Path.Combine(RootPath, fileName));
            MainPage.Log("删除文件名为:" + fileName);
        }
 
        /// <summary>
        /// Deletes all file.
        /// </summary>
        public static void DeleteAllFile()
        {
            List<string> 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}");
            }
        }
 
        /// <summary>
        /// Writes the file by bytes.
        /// </summary>
        /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns>
        /// <param name="fileName">File name.</param>
        /// <param name="bytes">Bytes.</param>
        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());
                }
            }
        }
 
        /// <summary>
        /// Writes the file by input stream.
        /// </summary>
        /// <returns><c>true</c>, if file by input stream was writed, <c>false</c> otherwise.</returns>
        /// <param name="fileName">File name.</param>
        /// <param name="inputStream">Input stream.</param>
        /// <param name="isNeedRootPath">If set to <c>true</c> is need root path.</param>
        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
                {
                }
            }
        }
 
        /// <summary>
        /// read file
        /// </summary>
        /// <returns>The file.</returns>
        /// <param name="fileName">File name.</param>
        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
                {
 
                }
            }
        }
 
        /// <summary>
        /// 下载电台、音乐、DLNA等等网络图片
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="url"></param>
        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();
                        }
                    }
                });
            }
        }
 
        /// <summary>
        /// 下载电台、音乐、DLNA等等网络图片
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="url"></param>
        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<Song>(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> album = new List<Album>();
        public List<Artist> artist = new List<Artist>();
        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;
    }
 
 
}