Question

In VS2008 I have writen a c# service, an installer and created a setup package to install it. The service needs to load an xml file to operate. Where is the best place to put this file in the various filesystem folders offered by the VS setup project, and how do I then refer to these paths from my code?

Thanks

[I should point out the the service runs as LocalService, which means that the applicationdata folder offered by the "User's Application Data Folder" item in the VS setup project is not accessible, even when "Install for all users" is used during installation. I could easily hack around this, but would like to understand best practice]

Was it helpful?

Solution

I am not sure which place is better to store the XML file. I don't think it will matter alot. But if you need to get special folder path in the system you can use Environment class to do so. The following line of code get the path of the Program Files:

string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

OTHER TIPS

To read installation path used by installer created from setup project:

1) Open "Custom Actions" editor in your setup project

2) Add custom action from your assembly where your installer class is located (If you haven't done so already)

3) Select this custom action and add /myKey="[TARGETDIR]\" to CustomActionData in property grid

4) In your Installer class you may access your value as follows: Context.Parameters["myKey"] in your method override dependent on your choice in step 2

This is a very old question, but since I disagree with the accepted answer, at least if the XML file will be updated by the program, I'll post this anyway.

What I do when installing a server-style program (Windows service or other non-user-specific program) is to install a default or template XML settings file in Program Files along with the program. But I never try to write to that file - this is typically not allowed.

Instead, during program initialization I test if the file has previously been copied to a sub-folder that I create under C:\ProgramData, i.e., "C:\ProgramData\myCompanyName\myProgramName\mySettingsFile.xml". (Find C:\ProgramData using Environment.SpecialFolder.CommonApplicationData - see here: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx). If the XML settings file already exists I just open it and use it - it is writable. If it does not exist, then I create the sub-folders if necessary and copy the template XML settings file from Program Files - this should be a one-time operation that is only done the first time the program is run after installation.

See here for more information: As a developer, how should I use the special folders in Windows Vista (and Windows 7)?

You could always use the registry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top