C#-从连接字符串获取文件路径 [英] C# - Get file path from connection string
问题描述
C#中是否存在现有方法,可将表示 ConnectionString
的字符串中的文件路径提取到 SqlCE
.sdf 文件?我想检查文件在初始化时是否存在,并备份文件是否已被修改。
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.
示例连接字符串:
strConn = "Data Source=|DataDirectory|\dbAlias.sdf";
推荐答案
也许有点晚,但是我遇到了这个问题苦苦挣扎着同样的问题。您可以使用 AppDomain.CurrentDomain.GetData( DataDirectory)
找到 | DataDirectory |
文件夹的位置。因此,您的连接字符串可以这样翻译:
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())
这篇关于C#-从连接字符串获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!