title: C# SQL Restore database to default data location
body: <p>I'm writing a C# application which downloads a compressed database backup via FTP.  The application then needs to extract the backup and restore it to the default database location.</p>
  
  <p>I will not know which version of SQL Server will be installed on the machine where the application runs. Therefore, I need to find the default location based on the instance name (which is in the config file).</p>
  
  <p>The examples I found all had a registry key which they read, but this will not work, since this assumes that only one instance of SQL is installed.</p>
  
  <p>Another example I found created a database, read that database's file properties, the deleting the database once it was done.  That's just cumbersome.</p>
  
  <p>I did find something in the .NET framework which should work, ie:</p>
  
  <p><pre><code>Microsoft.SqlServer.Management.Smo.Server(ServerName).Settings.DefaultFile</code></pre></p>
  
  <p>The problem is that this is returning empty strings, which does not help.</p>
  
  <p>I also need to find out the NT account under which the SQL service is running, so that I can grant read access to that user on the backup file once I have the it extracted.</p>