Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Get file path from connection string

Is there an existing method in C# to extract the file path from a string that represents a ConnectionString to a SqlCE .sdf file? I want to check if the file exists at initialization and back it up if the file has been modified.

Sample connection string:

strConn = "Data Source=|DataDirectory|\dbAlias.sdf";
like image 773
Nime Cloud Avatar asked Dec 07 '22 20:12

Nime Cloud


2 Answers

You can use SqlCeConnectionStringBuilder class to parse existing Sql Compact connection string.

like image 115
Giorgi Avatar answered Dec 24 '22 16:12

Giorgi


A bit late perhaps, but I came across this question wile struggling with the same problem. You can find the location of the |DataDirectory| folder with AppDomain.CurrentDomain.GetData("DataDirectory"). So your connectionstring can be translated like this:

strConn .Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString())
like image 45
Lodewijk Avatar answered Dec 24 '22 15:12

Lodewijk