locked
How to create a file in application data local folder which is not hidden in windows store app?

    Question

  • I want to create a log file in ApplicationData local folder.

         m_StorageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(m_Name.Replace(" ", "_") + ".log",
                              CreationCollisionOption.OpenIfExists);

    First time when my app is launched, the file gets created and logs are written.

    When I close my app, and re run the app once again, the log file that was created is now hidden and when i try to call

    the code :    m_StorageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(m_Name.Replace(" ", "_") + ".log",
                              CreationCollisionOption.OpenIfExists);

    m_StorageFile is NULL.

    How can i prevent the file from becoming Hidden or ReadOnly.

    Thanks

    Wednesday, December 3, 2014 11:55 AM

Answers

  • Here is a very simple sample that works fine.  Start with this and see what you are doing different.

    XAML:

    <Page
        x:Class="LocalStorage.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LocalStorage"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Loaded="Page_Loaded">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox  x:Name="outputTxt" HorizontalAlignment="Left" Margin="76,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" AllowDrop="True" PlaceholderText="No Data read from local file" Width="791"/>
            <Button Content="Store Data" HorizontalAlignment="Left" Margin="73,145,0,0" VerticalAlignment="Top" Click="Button_Click" />
    
        </Grid>
    </Page>
    

    C#

    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.Storage;
    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;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace LocalStorage
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                  if (m_StorageFile != null)
                  {
                      try
                      {
                          string userContent = outputTxt.Text;
                          if (!String.IsNullOrEmpty(userContent))
                          {
                              await FileIO.WriteTextAsync(m_StorageFile, userContent);
                              
                          }
                          else
                          {
                             // should be some info to write
                          }
                      }
                      catch (FileNotFoundException)
                      {
                          // do something
                      }
                  }
            }
    
         
            private StorageFile m_StorageFile;
            private async void createFileOrGetFile()
            { 
                m_StorageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("sample.log", CreationCollisionOption.OpenIfExists);
            }
            private async void Page_Loaded(object sender, RoutedEventArgs e)
            {
                createFileOrGetFile();
                if (m_StorageFile != null)
                {
                    try
                    {
                        string fileContent = await FileIO.ReadTextAsync(m_StorageFile);
                        outputTxt.Text = fileContent;
                    }
                    catch (FileNotFoundException)
                    {
                        // do something!
    
                    }
                }
            }
        }
    }
    


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, December 4, 2014 2:22 PM
    Moderator

All replies

  • m_Name isnt changing?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, December 3, 2014 12:02 PM
  • No. Its the same file name
    Wednesday, December 3, 2014 1:14 PM
  • What precisely do you mean when you say the file is hidden?

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, December 3, 2014 1:37 PM
    Moderator
  • After running the app for the first time, log file gets created and logs are written successfully.

    Later I close the app.

    When I re- run the application , ApplicationData.Current.LocalFolder.GetFilesAsync method should ideally give me the Log file which was created earlier. But when this API is called, no files are returned though the Log file still exists in the LocalState folder . When I check the properties of the log file, Hidden checkbox is ticked. I guess due to this, Im not able to get the file in my code for writing the logs once again.

    So, when i re-run the application 

     m_StorageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(m_Name.Replace(" ", "_") + ".log", CreationCollisionOption.OpenIfExists);

    returns a NULL value and my application runs into an ACCESS_DENIED exception on the launch of the app.

    Later When i manually un-check the HIDDEN checkbox of the log file, my app runs fine again.

    Kindly help me resolve this problem.

    Implemented based on this example : https://code.msdn.microsoft.com/windowsapps/Logging-Sample-for-Windows-0b9dffd7

    Thursday, December 4, 2014 6:59 AM
  • Here is a very simple sample that works fine.  Start with this and see what you are doing different.

    XAML:

    <Page
        x:Class="LocalStorage.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LocalStorage"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Loaded="Page_Loaded">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox  x:Name="outputTxt" HorizontalAlignment="Left" Margin="76,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" AllowDrop="True" PlaceholderText="No Data read from local file" Width="791"/>
            <Button Content="Store Data" HorizontalAlignment="Left" Margin="73,145,0,0" VerticalAlignment="Top" Click="Button_Click" />
    
        </Grid>
    </Page>
    

    C#

    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.Storage;
    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;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace LocalStorage
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                  if (m_StorageFile != null)
                  {
                      try
                      {
                          string userContent = outputTxt.Text;
                          if (!String.IsNullOrEmpty(userContent))
                          {
                              await FileIO.WriteTextAsync(m_StorageFile, userContent);
                              
                          }
                          else
                          {
                             // should be some info to write
                          }
                      }
                      catch (FileNotFoundException)
                      {
                          // do something
                      }
                  }
            }
    
         
            private StorageFile m_StorageFile;
            private async void createFileOrGetFile()
            { 
                m_StorageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("sample.log", CreationCollisionOption.OpenIfExists);
            }
            private async void Page_Loaded(object sender, RoutedEventArgs e)
            {
                createFileOrGetFile();
                if (m_StorageFile != null)
                {
                    try
                    {
                        string fileContent = await FileIO.ReadTextAsync(m_StorageFile);
                        outputTxt.Text = fileContent;
                    }
                    catch (FileNotFoundException)
                    {
                        // do something!
    
                    }
                }
            }
        }
    }
    


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, December 4, 2014 2:22 PM
    Moderator
  • Hi all,

    I am also facing exactly the same issue. Though my implementation is also same like the sample above but still hiding log file. Any idea?

    Monday, February 2, 2015 6:24 AM