Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get default backup path of sql server programmatically

I am taking backups of certain sql server databases programmatically using c#. I figured that Microsoft.SqlServer.Management.Smo and some other libraries are made for this purpose. Now I can backup a database. Very nice. Here is the code :

var server = new Server(@"" + InstanceName);
var backuper = new Backup();
try
{
    backuper.Action = BackupActionType.Database;
    backuper.Database = DbName;
    backuper.Devices.AddDevice(DbName + ".bak", DeviceType.File);
    backuper.BackupSetName = DbName + " - Yedek";
    backuper.BackupSetDescription = "Açık Bulut Depo - " + DbName + " - Yedek";
    backuper.ExpirationDate = DateTime.Now.AddYears(20);
    server.ConnectionContext.Connect();
    backuper.SqlBackup(server); 
}
catch(Exception ex){//..}

My question here is how can I get the path of the device that the database backed up into? I know I can specify my own path as :

backuper.Devices.AddDevice("C:\SOMEPATH\" + DbName + ".bak", DeviceType.File);

Then I can actually know where it is, but what I want to do is back it up to its default location and get its path. Please help me out with this.

like image 668
Tolga Evcimen Avatar asked Jul 11 '13 11:07

Tolga Evcimen


1 Answers

Correct Answer to this duplicate can be found here: https://stackoverflow.com/a/8791588/331889

Server.BackupDirectory;

Given you are already using SMO Objects it should be the simplest answer.

like image 63
Adam Hardy Avatar answered Oct 03 '22 11:10

Adam Hardy