-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSample.cs
More file actions
48 lines (37 loc) · 1.19 KB
/
Copy pathFileSample.cs
File metadata and controls
48 lines (37 loc) · 1.19 KB
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
using System;
using System.IO;
using System.Threading.Tasks;
namespace IT.Samples;
public class FileSample
{
public static async Task RunAsync()
{
Console.WriteLine("\nFileSample");
var content = $"content {Guid.NewGuid()}";
var path = Path.Combine(Environment.CurrentDirectory, "Files", "file.txt");
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
await WriteAllTextAsync(path, content).ConfigureAwait(false);
var readed = await ReadAllTextAsync(path).ConfigureAwait(false);
if (!readed.Equals(content)) throw new InvalidOperationException();
Console.WriteLine(readed);
}
private static Task WriteAllTextAsync(String path, String contents)
{
#if NETSTANDARD2_0
Console.WriteLine("_File");
return _File.WriteAllTextAsync(path, contents);
#else
return File.WriteAllTextAsync(path, contents);
#endif
}
private static Task<String> ReadAllTextAsync(String path)
{
#if NETSTANDARD2_0
Console.WriteLine("_File");
return _File.ReadAllTextAsync(path);
#else
return File.ReadAllTextAsync(path);
#endif
}
}