none
[UWP]clear app local folder. RRS feed

  • Question

  • Hi

    I'm making an app that requires a user to get a picture form their library, then saves to the local app storage folder. Here is my code so far:

    private async void button_Click(object sender, RoutedEventArgs e)
            {
                fileImage = await imagePicker.PickSingleFileAsync();
                if (fileImage != null)
                {
                    await fileImage.OpenAsync(FileAccessMode.Read);
                    StorageFolder temp = ApplicationData.Current.LocalFolder;
                    var file2 = await fileImage.CopyAsync(temp);
    
                    text1.Text = file2.Path.ToString();
    
                    string filename1 = file2.Path.ToString();
    
                    using (IRandomAccessStream fileStream = await fileImage.OpenAsync(FileAccessMode.Read))
                    {
                        WriteableBitmap bitmap = new WriteableBitmap(1, 1);
    
                        await bitmap.SetSourceAsync(fileStream);
                        image1.Source = bitmap;
                    }  
                }
            }
    So when I try to select the file again, the app crashes since the file exists. Is there a way I can clear the app local folder's contents when the app opens? or is there a way to automatically replace the file?


    Wednesday, May 11, 2016 12:25 AM

Answers

  • Hi WilltheRookieDev,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

    I have added a general tag to title for you this time. If it's not very correct with your project's type, you could edit it. And you would need to add tag by yourself next time.

    Add to pkr’s suggestion, you could set the “NameCollisionOption” for the “Copy CopyAsync” method when you call “CopyAsync” method. You could try my following code:

    var file2 = await fileImage.CopyAsync(temp,fileImage.Name,NameCollisionOption.ReplaceExisting);

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 12, 2016 5:47 AM
    Moderator

All replies

  • look at the FileAccessMode choices

    http://pauliom.com

    Wednesday, May 11, 2016 4:34 AM
  • Hi WilltheRookieDev,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

    I have added a general tag to title for you this time. If it's not very correct with your project's type, you could edit it. And you would need to add tag by yourself next time.

    Add to pkr’s suggestion, you could set the “NameCollisionOption” for the “Copy CopyAsync” method when you call “CopyAsync” method. You could try my following code:

    var file2 = await fileImage.CopyAsync(temp,fileImage.Name,NameCollisionOption.ReplaceExisting);

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 12, 2016 5:47 AM
    Moderator