title: Creating Database - How can I find out the Default Data Directory?
body: <p>I want to be able to create a database within my C# WinForm application using the code I found <a href="http://support.microsoft.com/kb/307283" rel="nofollow">HERE</a>.</p>
  
  <p>But I need to find a way to get the default data directory for that particular SQL Server Instance. I am wondering if there was a easy way to accomplish this that is able to be used on the various versions of SQL Server. </p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I found the following Select that will return the default data directory on the remote server:</p>
  
  <pre><code>SELECT 
      SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
  FROM master.sys.master_files 
  WHERE database_id = 1 
      AND file_id = 1
  </code></pre>
  
  <p>This solution will only work on SQL Server 2005+.</p>
  
  <p>**</p>