I have a web application that I'll be distributing to customers. I'll give them the option of backing up and restoring the back-end SQL Server database through an admin web page.
For backup I just programmatically create a file name using time-stamps and let SQL Server save it in the default backup folder. For restoring, I'd like to list the backup files and let the user choose which one to use, but I don't know what the default backup folder is to get a file listing.
How can I programmatically obtain the default backup folder for SQL Server 2008 Express R2 using C#?
Thanks
Try to adopt this stub:
The following query should give you the physical device name (or path) for each database. You can tailor it to suit your needs:
To retrieve the Backup path for a given server you could use the SQL Server Management Objects. The
Server
object has a property called BackupDirectory.You'll want something like this:
For this to work you will need to import usings/references for:
You will find more information here on how to interact with the SQL Server Management Objects.
I ended up using the following SqlCommand ... This solution appeared in another answer that was for some reason erased.? Anyway, here is the command I ended up using:
And here is the command in my C# code which shows the default backup folder path getting stored in the variable backupFolder:
The value you're looking for is stored in the registry
Note that the key depends on your SQL Server instance name.