StorageFile contain data or not (windwos phone)

52 Views Asked by At

How to get StorageFile file contain data ? if not then

AreaTextBlock.Text = "1.8;"

my current code is:

private async void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
      StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("text.txt", CreationCollisionOption.OpenIfExists);
      await FileIO.WriteTextAsync(file, "1.9");
}

private async void SettingsPage_Loaded(object sender, RoutedEventArgs e)
{
      StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("text.txt", CreationCollisionOption.OpenIfExists);
      AreaTextBlock.Text = await FileIO.ReadTextAsync(file);

}

on button tap AreaTextBlock.Text is 1.9 but i need when button is never tapped then AreaTextBlock.Text should be 1.8

i didn't want to write AreaTextBlock.Text = "1.8" in xaml or in c# initialize because when exit and relaunch AreaTextBlock.Text is 1.8 even if its text is 1.9. so how to do that

1

There are 1 best solutions below

1
Martin Zikmund On BEST ANSWER

Basically all you need is to check at the end of the Loaded method if the AreaTextBox.Text is empty and if yes, set the default value.

if ( AreaTextBox.Text == "" ) 
{
      AreaTextBox.Text = "1.8";
}