Last active
March 27, 2016 17:16
-
-
Save AlexGladkov/4a73dfe28995d0e9096e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task WriteToFile(string key, string foldername, string filename) | |
{ | |
// Get the text data from the textbox. | |
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(key.ToCharArray()); | |
// Get the local folder. | |
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; | |
// Create a new folder name DataFolder. | |
var dataFolder = await local.CreateFolderAsync(Path.Combine("DataFolder", foldername), CreationCollisionOption.OpenIfExists); | |
// Create a new file named DataFile.txt. | |
var file = await dataFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); | |
// Write the data from the textbox. | |
using (var s = await file.OpenStreamForWriteAsync()) | |
{ | |
s.Write(fileBytes, 0, fileBytes.Length); | |
} | |
} | |
public static async Task<string> ReadFile(string foldername, string filename) | |
{ | |
// Get the local folder. | |
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; | |
if (local != null) | |
{ | |
Stream file = null; | |
// Get the DataFolder folder. | |
var dataFolder = await local.GetFolderAsync(Path.Combine("DataFolder", foldername)); | |
// Get the file. | |
file = await dataFolder.OpenStreamForReadAsync(filename); | |
string key; | |
// Read the data. | |
using (StreamReader streamReader = new StreamReader(file)) | |
{ | |
return key = streamReader.ReadToEnd(); | |
} | |
} | |
else | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment