0

I have to make a backup of a database through this code which must have access to the File System.

I added the references to the Package.appxmanifest as from this link and I also activated the app permission in the settings.

Settings -> Privacy -> File System and I have activated the app permission. This way you should have access to the files through the paths but it still crashes.

MainPage.xaml.cs:

string constring = "server=localhost;user=user;pwd=password;database=dbtest;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(file);
            conn.Close();
        }
    }
}

Package.appxmanifest:

  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">

  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="broadFileSystemAccess" />
  </Capabilities>

The error is:

Access to the path C:\backup.sql' is denied.

Is there anything I overlook?

Thanks in advance.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Grusi
  • 1
  • 1
  • UWP apps are limited and cannot be run as administrators. that said, you cannot do what you want in a UWP app. https://stackoverflow.com/questions/36286806/uwp-limitations-in-desktop-apps – Paulo Alves Dec 27 '19 at 11:36
  • Maybe this post could help you: https://stackoverflow.com/questions/19724297/asp-net-getting-the-error-access-to-the-path-is-denied-while-trying-to-upload – dino Dec 27 '19 at 11:40
  • But at this point it is possible to use MySqlBackup in UWP? – Grusi Dec 27 '19 at 12:29
  • Hi @Grusi , Enabling `broadFileSystemAccess` can indeed access the file through the path, but it needs to be accessed through the method under the `System.IO` namespace. Some APIs used by `MySqlBackup` for file access are not supported and can only be used in Win32 applications. – Richard Zhang Dec 27 '19 at 12:50
  • Check Settings->Privace->File System->Choose which apps can access your filesystem and make sure your app is set to "On". – Stefan Wick MSFT Dec 27 '19 at 17:19

1 Answers1

2

The problem in this case is with the MySqlBackup.ImportFromFile method, which in fact uses System.IO APIs under the hood, which cannot access an arbitrary path even when broadFileSystemAccess is enabled. To be able to access any filesystem path, you need to use the Windows.Storage APIs instead (StorageFile and StorageFolder APIs).

To make it work, you must use other methods MySqlBackup offers - either ImportFromMemoryStream or ImportFromString. For example:

string constring = "server=localhost;user=user;pwd=password;database=dbtest;";
string file = "C:\\backup.sql";
var fileContent = await Windows.Storage.FileIO.ReadTextAsync(file);
using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromString(fileContent);
            conn.Close();
        }
    }
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91