WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
FileHelper.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System.IO;
6using System.Threading;
7using System.Threading.Tasks;
8
9namespace WGetNET.Helper
10{
14 internal static class FileHelper
15 {
46 public static void WriteTextToFile(string path, string text)
47 {
48 string? directory = Path.GetDirectoryName(path);
49 if (directory != null && !Directory.Exists(directory))
50 {
51 Directory.CreateDirectory(directory);
52 }
53
54 File.WriteAllText(path, text);
55 }
56
90 public static async Task WriteTextToFileAsync(string path, string text, CancellationToken cancellationToken = default)
91 {
92 if (cancellationToken.IsCancellationRequested)
93 {
94 return;
95 }
96
97 string? directory = Path.GetDirectoryName(path);
98 if (directory != null && !Directory.Exists(directory))
99 {
100 Directory.CreateDirectory(directory);
101 }
102
103#if NETCOREAPP3_1_OR_GREATER
104 await File.WriteAllTextAsync(path, text, cancellationToken);
105#elif NETSTANDARD2_0
106 // Delete file to recreate it
107 if (File.Exists(path))
108 {
109 File.Delete(path);
110 }
111 // Create file and write the string to the stream
112 using StreamWriter fileStream = File.CreateText(path);
113 await fileStream.WriteAsync(text);
114 fileStream.Close();
115#endif
116 }
117 }
118}