3

i would like some help with a program i am developing, i want the user to be able to set the database connection string from a form i have developed. I have seen many articles on this, including many answers on stack overflow that all worked but none of them permanently write the connection to the app.config file, meaning when the application is closed the connection string retains it's former value (during debugging), i want to know if there is a way i can solve this issue.

I have already tried this: How can i update app.config connectionstring Datasource value in C#?

And this is what i am currently using:

http://www.codeproject.com/Questions/388444/How-change-connection-string-dynamically-in-csharp See Solution 2

Community
  • 1
  • 1
Blank EDjok
  • 732
  • 2
  • 14
  • 33
  • 2
    Try following answer: http://stackoverflow.com/a/1357453/1661209 – Max Feb 20 '14 at 11:24
  • 2
    if this issue is for debugging only then it is because configuration file is updated when you build your project, and this issue will be no more when you deploy it. – Milan Raval Feb 20 '14 at 11:26
  • You will need to rewrite the configuration file, and restart the app. – Grant Thomas Feb 20 '14 at 11:28
  • @Milan Raval Interesting, so are you then suggesting that when the application is packaged and deployed unto the client's machine once he sets the connection string once it will NOT change again even if he restarts the application? – Blank EDjok Feb 20 '14 at 11:30
  • 1
    One problem with trying to change the app.config is that you might not have the permission to do so if the application is installed in a protected folder like `Program Files`. – Dirk Feb 20 '14 at 11:37
  • Interesting so is there a way of setting folder permissions or access before i can do this, or will it just display the popup asking the user whether to grant the application access? – Blank EDjok Feb 20 '14 at 11:40
  • @Blank EDjok: to check this what you can do is, start application.exe from your debug folder and see whether the change pertains or not. I also agree with Dirk that you should take care of permission also. – Milan Raval Feb 20 '14 at 11:51
  • Okay thank you all for your contributions, i will check on that. – Blank EDjok Feb 20 '14 at 11:55

1 Answers1

1

Assuming this is a Forms/Console application called WindowsFormsApplication1:

When running your application in Visual Studio in Debug mode, the file that will be updated is:

bin\Debug\WindowsFormsApplication1.vshost.exe.Config

Each time you terminate your debugging session this file will then revert back to whatever is contained in your project's app.config file.

There are reasons for this behaviour which I don't have time to explain right now.

When you run your application standalone i.e. not within Visual Studio then the app.config file deployed with your app binaries will be the file that gets updated.

For example, I copied the output of my bin\Release folder (excluding any *.vshost.* files) to another directory outside the project structure, launched the app and sure enough my config changes do get saved to app.config.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • Oho!! Thanks i get it now, so i can't really permanently update the settings in my projects app.config file during debug mode then. – Blank EDjok Feb 20 '14 at 12:07
  • That seems to be the case. In my head I understand why, but right at this moment don't have time to explain it :) – Kev Feb 20 '14 at 12:08