|
| 1 | +using System.IO; |
| 2 | +using System.Threading.Tasks; |
| 3 | + |
| 4 | +namespace Shield.Client.Fr.Extensions |
| 5 | +{ |
| 6 | + public static class FileExtensions |
| 7 | + { |
| 8 | + /// <summary> |
| 9 | + /// Save downloaded application as stream to path. |
| 10 | + /// </summary> |
| 11 | + /// <param name="downloadedApplication"></param> |
| 12 | + /// <param name="path"></param> |
| 13 | + /// <param name="replaceIfExist">Replace if the path contains an existing file.</param> |
| 14 | + /// <returns></returns> |
| 15 | + public static async Task SaveOnAsync(this Stream downloadedApplication, string path, bool replaceIfExist = false) |
| 16 | + { |
| 17 | + if(File.Exists(path)) |
| 18 | + if (replaceIfExist) |
| 19 | + File.Delete(path); |
| 20 | + else return; |
| 21 | + |
| 22 | + using var fileStream = File.Create(path); |
| 23 | + downloadedApplication.Seek(0, SeekOrigin.Begin); |
| 24 | + await downloadedApplication.CopyToAsync(fileStream); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Save downloaded application as stream to path. |
| 29 | + /// </summary> |
| 30 | + /// <param name="downloadedApplication"></param> |
| 31 | + /// <param name="path"></param> |
| 32 | + /// <param name="replaceIfExist">Replace if the path contains an existing file.</param> |
| 33 | + /// <returns></returns> |
| 34 | + public static void SaveOn(this Stream downloadedApplication, string path, bool replaceIfExist = false) |
| 35 | + { |
| 36 | + if(File.Exists(path)) |
| 37 | + if (replaceIfExist) |
| 38 | + File.Delete(path); |
| 39 | + else return; |
| 40 | + |
| 41 | + using var fileStream = File.Create(path); |
| 42 | + downloadedApplication.Seek(0, SeekOrigin.Begin); |
| 43 | + downloadedApplication.CopyTo(fileStream); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Save downloaded application as array to path. |
| 49 | + /// </summary> |
| 50 | + /// <param name="downloadedApplication"></param> |
| 51 | + /// <param name="path"></param> |
| 52 | + /// <param name="replaceIfExist">Replace if the path contains an existing file.</param> |
| 53 | + /// <returns></returns> |
| 54 | + public static void SaveOn(this byte[] downloadedApplication, string path, bool replaceIfExist = false) |
| 55 | + { |
| 56 | + if (File.Exists(path)) |
| 57 | + if (replaceIfExist) |
| 58 | + File.Delete(path); |
| 59 | + else return; |
| 60 | + |
| 61 | + File.WriteAllBytes(path, downloadedApplication); |
| 62 | + |
| 63 | + } |
| 64 | + /// <summary> |
| 65 | + /// Save downloaded application as array to path. |
| 66 | + /// </summary> |
| 67 | + /// <param name="downloadedApplication"></param> |
| 68 | + /// <param name="path"></param> |
| 69 | + /// <param name="replaceIfExist">Replace if the path contains an existing file.</param> |
| 70 | + /// <returns></returns> |
| 71 | + public static async Task SaveOnAsync(this byte[] downloadedApplication, string path, bool replaceIfExist = false) |
| 72 | + { |
| 73 | + await Task.Run(() => |
| 74 | + { |
| 75 | + if (File.Exists(path)) |
| 76 | + if (replaceIfExist) |
| 77 | + File.Delete(path); |
| 78 | + else return; |
| 79 | + |
| 80 | + // Netstandar 2.2 |
| 81 | + //File.WriteAllBytesAsync(path, downloadedApplication); |
| 82 | + |
| 83 | + File.WriteAllBytes(path, downloadedApplication); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments