Accessing and modifying user settings between applications in VB.Net

Recently and for the first time I coded a Windows service. A basic service that runs in the background and based upon a set interval would poll all the PC’s and servers on my network looking for archived event logs. I set my logs to save when they reach 16mb. On some boxes due to the amount of auditing I do this was consuming hard drive space pretty rapidly and I needed a simple way to retrieve those logs, zip them up and move them to a folder that gets backed up to the cloud weekly.

Of course as I progressed my service needed some “Settings”. For example the time interval I mentioned, the folder to move the zipped files to, etc. I thought no big deal, I have some canned code with a property grid already setup I will just copy and paste then I can run my service from the command line with a switch to show the dialog. Yea figured out that won’t work. Since its my first service I am learning about the do’s, dont’s and the “that’s not happening” issues. The latter being my problem. Seems like you can’t just run a service from the command line like I thought.

Okay no big deal I will just code a small project just for editing those settings in my service. There it was again, my naivety . I did some research on the web, tried a few suggestions here and there with no luck. Then I landed on a page that someone obviously smarter than I told the group you cannot access other project user settings with vb.net. He went on to explain that the project stores the user settings in some folder with a unique hash and that it was impossible to use.

Huh, sounds like a challenge.

Well first what he said did not make sense. Of the nearly 100 projects I have coded through the years, the projects settings file is in the same folder as the EXE itself. To confirm that my service was using the settings file within its running folder I made some changes in the settings file with notepad and restarted my service. Yup using event viewer I could see my service was in fact using that file. But why? If indeed the the settings were constructed in the project settings as having a scope of “user” then that file would have to be unique for anyone that logs onto that machine, right? Well I went on to investigate some more and his explanation seems to hold water but only for Windows Forms applications and not a service application. User settings are stored in something like this: “C:\Users\gutauckis.CITYOFHOLLYHILL\AppData\Local\Hewlett-Packard Company\Settings\1.0.0.0” and a service is not “user” based. So those settings would not be stored with a user but in the applications directory.

Here is a link to a good explanation the settings architecture 

In his explanation he mentioned just storing your settings in an XML file that both of the projects would know where they are stored. Then you can use one project, the GUI to modify the settings and the service would read them at startup or during code execution. That sounds like a good idea. Since my settings file is stored with the service application and my setting gui will be in the same folder I can just read the settings file, make changes and save it back out.

Here is the code for reading and writing the settings xml file:

To use it:

That’s pretty much it. Nothing fancy. Looking at the xml reader I could probably clean it up, but its working so I will leave it alone for now.

Remember this is for a service application, not a windows forms application. You could do the same thing only you will need to know where the users application setting is stored and share that with your setting GUI application.

Run your forms app and get the Application.LocalUserAppDataPath Property. Then add that to your settings GUI app so it knows where to look. Easy enough.

Leave a Reply