1

I have an SQL Server CE 4.0 (SDF) file that under the Users Application Data Folder (because due to Win7 UAC you cannot modify the DB if it is in the \Programs Files\ folder. But the problem remains that I need to now (on install) modifiy the .EXE.CONFIG file (ConnectionString) to point to the new path for the DB, however obviously I do not have access to modify the .EXE.CONFIG file either in the \Program Files\ folder ...

So how do people resolve this issue? Can you deploy the .EXE.CONFIG file to the users Application Data Folder as well with the SDF file? If not how? I cannot beleive you need to grant special access rights to the \Program Files\Application folder to be able to have full access...

I am using VS2010 and using a standard Deployment Project (MSI) and I don't see anyway to deploy the .EXE in one place the the .EXE.CONFIG in another (can this be done? Is this the right solution?)

Thanks,

JSchwartz
  • 2,536
  • 6
  • 32
  • 45

1 Answers1

1

Use the %APPDATA% variable in the connection string and you won't have to modify the configuration file. This is also more flexible than hard-coding a path that contains a username, since it will allow all users of the computer to use your program and have their own .sdf files.

However, the environment variables are not automatically expanded in .NET, so you need to enforce it by using the Environment.ExpandEnvironmentVariable method:

// Example appSetting element:
//   <add key="examplePath" value="%APPDATA%\example.txt" />

string path = System.Configuration.ConfigurationManager.AppSettings["examplePath"].ToString();
path = Environment.ExpandEnvironmentVariables(path);
System.IO.File.WriteAllText(path, "foobar");
Community
  • 1
  • 1
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
  • Odly enough that doesn't seem to work, it stays that way (doesn't resolve) ... the solution I had was in-code (on init) read in the connection string and if you find %APPDATA% then replace it (string replace) with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) – JSchwartz Feb 23 '13 at 01:33
  • @JSchwartz Thanks for pointing that out! I posted my answer without writing the code to verify it, but I have just made up for that, learned something new and edited the answer. – Marek Grzenkowicz Feb 23 '13 at 11:31