I have tried to use the windows app documentation to read temporary files (read and write) on UWP. But I couldn't make it work using the documentation as in the example at this link below
Documentation in this parte:
async void WriteTimestamp()
{
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt",
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTimeOffset.Now));
}
But soon I have a problem with "temporaryFolder". It's says doesn't exist in the current context. So... I try: (1) add "using Windows.Storage;" don't work... (2) declare outside:
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
this time the code says there is no error, but when I start the program it stops. " 'Object reference not set to an instance of an object.' App12UIWindows.Page2.temporaryFolder.get return null."
So I tried a solution from here (stackoverflow) and it worked. Link: Alternative Solution
My question is: why it go wrong? the documentation is correct or is it something else? or am I searching in the wrong place? My code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using Windows.Storage;
using System.Threading.Tasks;
namespace App12UIWindows
{
/// <summary>
/// </summary>
public sealed partial class Page2 : Page
{
public Page2()
{
this.InitializeComponent();
SaveTempFunc();
readMyFileTemp();
}
public Windows.Storage.StorageFolder temporaryFolder { get; set; }
async void SaveTempFunc()
{
StorageFile salvandoClientesT = await
temporaryFolder.CreateFileAsync("textOf_Information.txt") ;
await FileIO.WriteTextAsync(salvandoClientesT,"test .. just like that... and
anothers words to a file");
}
//Fim salvando memória temporária
//Lendo na memória temporária arquivo de texto
async void readMyFileTemp()
{
try
{
StorageFile lendoTextoSalvo = await
temporaryFolder.GetFileAsync("textOf_Information.txt");
String timestamp = await FileIO.ReadTextAsync(lendoTextoSalvo);
}
catch (Exception)
{
}
}
}
}
Sorry for the bad english :)