UWP C# - Access mobile phone files

1

I am writing some kind of file synchronisation UWP program. This app will run on a Windows desktop and synchronize files to my mobile device, an android phone. My phone is plugged on my dev PC on the USB port and is seen as "My Device" in file explorer. From file explorer, I can access the main storage of my phone and create a file.

In my app manifest, I added a file type association declaration for files with extension ".profile".

When my application try to create the profile file "myprofile.profile" with the following code, it fails with a COMException 0x80004005 who correspond to an access denied error. The profileDevice variable is of type StorageDevice.

StorageFile profileFile = await profileDevice.RootFolder.CreateFileAsync("myProfile.profile", CreationCollisionOption.OpenIfExists);

Any idea if what I want to do is possible and if so, how?

c#
mobile
uwp
access-denied
asked on Stack Overflow Mar 13, 2019 by sbeaudoin • edited Mar 14, 2019 by sbeaudoin

1 Answer

0

Before synchronize files to removable storage. you need declare the specific file type. if your file type is .txt, you need add type extension name to File Type Associations capability.

enter image description here

For more please refer this document

Please note, because the file transfer protocol is MTP, so you could not use CreateFileAsync method. please try to use CopyAsync method and copy the file from your computer to your phone.

StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices; 
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();

if (sdCard != null)
{
    var rootFolder = await sdCard.GetFoldersAsync();
    var folder = rootFolder[0];
    var picker = new FileOpenPicker
    {
        FileTypeFilter = { ".jpg", ".png", ".gif", ".txt" },
        SuggestedStartLocation = PickerLocationId.PicturesLibrary
    };
    var sourceFile = await picker.PickSingleFileAsync();
    if (sourceFile != null)
    {
        var newfile = await sourceFile.CopyAsync(folder, sourceFile.Name, NameCollisionOption.ReplaceExisting);
    }
}
else
{
    // No SD card is present.
}
answered on Stack Overflow Mar 15, 2019 by Nico Zhu - MSFT • edited Mar 15, 2019 by Nico Zhu - MSFT

User contributions licensed under CC BY-SA 3.0