locked
Read and write a text file

    Question

  • I am getting a error with this code I just want to read and write a file I tried the sample from MSDN dint understand much please help me

    StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                rootpage.sampleFile = await storageFolder.CreateFileAsync(Settings.filename, CreationCollisionOption.ReplaceExisting);
                OutputTextBlock.Text = "The file '" + rootpage.sampleFile.Name + "' was created.";


    pratik


    • Edited by Pratik99 Tuesday, April 1, 2014 6:27 AM
    Tuesday, April 1, 2014 6:26 AM

Answers

  • Sir now I am trying to do this code please do help me it is showing file not found

     private async void Save_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder folder = KnownFolders.PicturesLibrary;
                StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
                await Windows.Storage.FileIO.WriteTextAsync(sampleFile,textbox.text);
                
                
            }
    
    
            private async void Read_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
                string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
                mntnc.Text = text;
    
    
    
              
            }


    pratik

    • Marked as answer by Pratik99 Tuesday, April 1, 2014 6:22 PM
    Tuesday, April 1, 2014 2:49 PM
  • Got it I was using the Picture Library instead of the document library 

    pratik

    • Marked as answer by Pratik99 Tuesday, April 1, 2014 6:22 PM
    Tuesday, April 1, 2014 6:22 PM

All replies

  • What error are you getting and where?

    What about the sample confused you?

    Does your app have the pictures library capability?

    Tuesday, April 1, 2014 6:33 AM
    Owner
  • Hi Sir,

    Now I have modified the code when the textbox is getting loaded the saved text is not showing up

     private async void Save_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                sampleFile = await storageFolder.CreateFileAsync(Settings.filename, CreationCollisionOption.ReplaceExisting);
                OutputTextBlock.Text = "The file '" + sampleFile.Name + "' was created.";
                
                StorageFile file = sampleFile;
                if (file != null)
                {
                    try
                    {
                        string userContent = tech.Text;
                        if (!String.IsNullOrEmpty(userContent))
                        {
                            await FileIO.WriteTextAsync(file, userContent);
                            OutputTextBlock.Text = "The following text was written to '" + file.Name + "':" + Environment.NewLine + Environment.NewLine + userContent;
                        }
                        else
                        {
                            OutputTextBlock.Text = "The text box is empty, please write something and then click 'Write' again.";
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        
                    }
                }
                else
                {
                   
                }
            }
    
            private async void textbox_Loaded(object sender, RoutedEventArgs e)
            {
                StorageFile file = sampleFile;
                if (file != null)
                {
                    try
                    {
                        string fileContent = await FileIO.ReadTextAsync(file);
                        OutputTextBlock.Text = "The following text was read from '" + file.Name + "':" + Environment.NewLine + Environment.NewLine + fileContent;
                    }
                    catch (FileNotFoundException)
                    {
                        OutputTextBlock.Text = "The following text was read from '"; 
                    }
                }
                else
                {
                    OutputTextBlock.Text = "The following text was read from xyz '"; 
                }
            


    pratik

    Tuesday, April 1, 2014 6:39 AM
  • Sir now I am trying to do this code please do help me it is showing file not found

     private async void Save_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder folder = KnownFolders.PicturesLibrary;
                StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
                await Windows.Storage.FileIO.WriteTextAsync(sampleFile,textbox.text);
                
                
            }
    
    
            private async void Read_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
                string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
                mntnc.Text = text;
    
    
    
              
            }


    pratik

    • Marked as answer by Pratik99 Tuesday, April 1, 2014 6:22 PM
    Tuesday, April 1, 2014 2:49 PM
  • Got it I was using the Picture Library instead of the document library 

    pratik

    • Marked as answer by Pratik99 Tuesday, April 1, 2014 6:22 PM
    Tuesday, April 1, 2014 6:22 PM