-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileHelper.cs
49 lines (43 loc) · 1.44 KB
/
FileHelper.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
namespace PhotoLibraryApp
{
public static class FileHelper
{
public async static void WriteTextFileAsync(string filename, string content)
{
var storageFolder = ApplicationData.Current.LocalFolder;
var textFile = await storageFolder.CreateFileAsync(filename,
CreationCollisionOption.OpenIfExists);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(textFile.Path, true))
{
file.WriteLine(content);
}
}
public async static Task<string> ReadTextFileAsync(string filename)
{
var storageFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile;
try
{
textFile = await storageFolder.GetFileAsync(filename);
}
catch (FileNotFoundException)
{
return null;
}
var textStream = await textFile.OpenReadAsync();
var textReader = new DataReader(textStream);
var textLength = textStream.Size;
await textReader.LoadAsync((uint)textLength);
return textReader.ReadString((uint)textLength);
}
}
}