0

I'm working in a UWP app, at some point, the user has the chance to save an image (done) by now I only let the user know that the save operation was done by a "messageDialog", this is my code:

        var savePicker = new Windows.Storage.Pickers.FileSavePicker();
        savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
        
        savePicker.FileTypeChoices.Add("QR Code", new List<string>() { ".png", ".jpg", ".jpeg", ".jpe", ".bmp" });
        
        savePicker.SuggestedFileName = "QR_01";
        Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
        if (file != null)
        {
            
            Windows.Storage.CachedFileManager.DeferUpdates(file);
            
            await Windows.Storage.FileIO.WriteBytesAsync(file, _bytes3);
            
            Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
            
            if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
            {
                var messageDialog = new Windows.UI.Popups.MessageDialog("File saved on: " + file.Path);

                await messageDialog.ShowAsync();
            }
            else
            {
                //this.textBlock.Text = "File " + file.Name + " couldn't be saved."; not defined by now
            }
            
            done = true;
        }
        else
        {
            this.textBlock.Text = "operation canceled";
            done = false;
        }

This works good so far but I want to be sure that the file is already there after the "WriteBytesAsync" method. So, this is the thing, the users can save the image in any directory they have acces to so I can't check (or read) the folder they choose afer that because I don't have acces permission afer save the file. I have tryed something like this to get folder access:

        string savePath = System.IO.Path.GetDirectoryName(file.Path);
        Windows.Storage.StorageFolder saveFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(savePath);

In order to use the "GetFileAsync" to check the file or the "FutureAccessList" for a future access but since I have file access permisions to any directory it fails, and because of that I can't use any of the solution in here: UWP Check If File Exists

My Questions are:

1.- Is there a way to check if the file was saved or successfully written afer the user click the "Save" button?

2.- Does the "WriteBytesAsync" method is enough to ensure taht the file was saved?? according to the info FileIO.WriteBytesAsync(IStorageFile, Byte[]) Method, it don't throw any exception.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • WriteBytesAsync will throw if there are issues. Seeing as you have the actual file, you could read it back after writing to check it exists, or ask Windows for it's fileSize and check it's not 0 bytes. But WriteBytesAsync completing should be enough as writes are transacted and the file stream is flushed before returning. See https://learn.microsoft.com/en-us/windows/uwp/files/best-practices-for-writing-to-files – Johnny Westlake Mar 10 '21 at 11:27
  • Very useful info by @JohnnyWestlake for anyone dealing with FileIO and PathIO classes. Thanks – Marckozz Gg Mar 10 '21 at 21:11

1 Answers1

1

but I want to be sure that the file is already there after the "WriteBytesAsync" method.

For you requirement, you could use GetFileFromPathAsync method to get the file, if the file is not null, means that the file is exist. And please note if you want to above method, you need add broadFileSystemAccess capability, and open it in your system setting. For more please refer this.

Windows.Storage.CachedFileManager.DeferUpdates(file);
await Windows.Storage.FileIO.WriteBytesAsync(file, new byte[3]);
var temp = await StorageFile.GetFileFromPathAsync(file.Path);
if(temp != null)
{

}
else
{

}

Does the "WriteBytesAsync" method is enough to ensure taht the file was saved??

Yep, WriteBytesAsync method need to pass StorageFile parameter. if the file parameter is not null means it has created in the folder. Or it will throw null exception.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • 1
    Thanks for the answer, the use "GetFileFromPathAsync" could works, however, is the same situation of using "GetFolderFromPathAsync(savePath)" since I don't have access permissions, as you say, I'd need to add "broadFileSystemAccess" and I wouldn't like to have my app submission rejected for a bad justification haha. – Marckozz Gg Mar 10 '21 at 06:38
  • Yep, I think your second question has pointed the key. The file has been created in the in the storage, when `PickSaveFileAsync` has finished and the return value is not null . – Nico Zhu Mar 10 '21 at 06:48