Question

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,

Was it helpful?

Solution

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

OTHER TIPS

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top