Sasha Vasserfirer
Sasha Vasserfirer

Reputation: 69

Access path denied when backing up database using mysqlBackup

I'm trying to back up my database from mysql local server using this code:

string folder = DateTime.Now.Date.ToString("yyyy-MM-dd");                  
var root = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"Database backup");
var newFolderPath = Path.Combine(root, folder);

if (!Directory.Exists(newFolderPath))  // if it doesn't exist, create
    Directory.CreateDirectory(newFolderPath);

MySqlConnection myCon = frmStudentsSignup.establishConnectionToMysql();
using(MySqlCommand cmd = new MySqlCommand()) {
  using(MySqlBackup mb = new MySqlBackup(cmd)) {
    cmd.Connection = myCon;
    myCon.Open();
    mb.ExportToFile(newFolderPath);
    myCon.Close();
  }
}

After lunching this line

mb.ExportToFile(newFolderPath);

I get

access to the path ... is denied.

My path is located at visual studio project directory.

Also the creating of a new directory is working so I have no idea what could be wrong.

Upvotes: 0

Views: 518

Answers (2)

mjb
mjb

Reputation: 7969

You are trying to save as a folder, not file. Here is the fix:

string folder = DateTime.Now.Date.ToString("yyyy-MM-dd");
var root = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Database backup");
var newFolderPath = Path.Combine(root, folder);

if (!Directory.Exists(newFolderPath))  // if it doesn't exist, create
    Directory.CreateDirectory(newFolderPath);

// Fixed
string newFileFolderPath = Path.Combine(newFolderPath, "mybackup.sql");

MySqlConnection myCon = frmStudentsSignup.establishConnectionToMysql();
using (MySqlCommand cmd = new MySqlCommand())
{
    using (MySqlBackup mb = new MySqlBackup(cmd))
    {
        cmd.Connection = myCon;
        myCon.Open();
        mb.ExportToFile(newFileFolderPath);
        myCon.Close();
    }
}

Upvotes: 0

Svein Fidjestøl
Svein Fidjestøl

Reputation: 3206

Just a suggestion, but you can try with a trailing slash in the directory path, i.e. change the newFolderPath assignment line to

var newFolderPath = Path.Combine(root, folder) + Path.DirectorySeparatorChar;

If that doesn't help, try with a path that is short and contains no special characters like spaces or dashes (-), e.g. C:\testpath\

Upvotes: 0

Related Questions