I am copying a file from the photo gallery to my local app location for further processing.
Unfortunately, after the copy is made, I am unable to access the file.
The line that assigns baContent is where I receive the failure.
private async Task SaveAttachment()
{
if (SelectedChecklistItem?.AttachmentFilePath != null)
{
try
{
byte[] baContent;
baContent = await File.ReadAllBytesAsync(SelectedChecklistItem.AttachmentFilePath);
await _mySvc.SaveAttachment(baContent);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
The full error message is:
Could not find a part of the path '/Users/{username}/Library/Developer/CoreSimulator/Devices/87FA0141-768B-4C54-94C9-153508726112/data/Containers/Bundle/Application/26E81D7F-0B46-4022-8112-78B42FA9E546/{myappname}/File: /Users/{username}/Library/Developer/CoreSimulator/Devices/87FA0141-768B-4C54-94C9-153508726112/data/Containers/Data/Application/E90F78D7-53F6-4E11-87FC-C9ED1BB329A1/Documents/atp20240708155037.jpeg'.
When I browse to the location on the Mac, I am able to see the file in the location corresponding to the path after “File:” in the above error.
Here is the code that creates the file:
private async Task CopyToLocalAppStorage(string filePrefix, FileResult result)
{
if (result != null)
{
var attPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var fileExt = System.IO.Path.GetExtension(result.FullPath);
attachmentFilePath = Path.Combine(attPath, $"{filePrefix}{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}{fileExt}");
using (FileStream SourceStream = File.Open(result.FullPath, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(attachmentFilePath))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
I expected to be able to access the file at this point – since it is created by me in this app.
Work402fjp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.