I am trying to use C# to backup selected databases in my WPF application. I think the code is fine, but when the backup runs, I get this error:
Since I received that error I tried to give the folder permissions to everyone through C# but I am still having the same issue. Any help will be greatly appreciated. Thanks in advance.
Here is my code:
DirectorySecurity sec = Directory.GetAccessControl(backupFolder);
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(backupFolder, sec);
List<string> dbNameList = GetDatabaseList();
if (dbNameList != null)
{
SqlCommand oCommand = null;
SqlConnection oConnection = null;
foreach (string dbName in dbNameList)
{
string command = @"BACKUP DATABASE " + dbName + " TO DISK='" + backupFolder + "'";
oConnection = new SqlConnection(ConnectionString);
if (oConnection.State != ConnectionState.Open)
oConnection.Open();
oCommand = new SqlCommand(command, oConnection);
oCommand.ExecuteNonQuery();
}
oConnection.Close();
}