문제

I've been trying to set a connectionstring to the users programdata folder and followed the first step in the answer of this post:

%APPDATA% in connection string is not substituted for the actual folder?

Unfortunately I can't get it to work:

In the onstartup method of my WPF application I run the following:

AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

var test = AppDomain.CurrentDomain.GetData("DataDirectory");

var connection = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;

Value in App.config

<add name="Conn" connectionString="data source=|DataDirectory|\mydb" providerName="System.Data.SQLite" />

result of test = "c:\programdata" => this is good

result of connectionstring ="|DataDirectory|\mydb" => this is not good

I'm expecting: "c:\programdata\mydb"

I've been looking all around... what am I doing wrong?

thanks in advance,

도움이 되었습니까?

해결책

You are misunderstanding how it works.
Setting the DataDirectory is correct, but the actual connectionstring is not changed on file.
When you open the connection the |DataDirectory| part of the string will be replaced with your path

You just need to try and see by yourself

다른 팁

If you're following the answer you linked to, you seem to be missing the following from Step 2. in the link:

 return connection.Replace("%APPDATA%",
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top