Skip to content

Instantly share code, notes, and snippets.

@AlexGladkov
Last active March 27, 2016 17:16
Show Gist options
  • Save AlexGladkov/4a73dfe28995d0e9096e to your computer and use it in GitHub Desktop.
Save AlexGladkov/4a73dfe28995d0e9096e to your computer and use it in GitHub Desktop.
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