<p>fellow anthropoids and lily pads and paddlewheels!</p>

<p>I'm developing a Windows desktop app in C#/.NET/WPF, using VS 2008. The app is required to install and run on Vista and XP machines. I'm working on a Setup/Windows Installer Project to install the app.</p>

<p>My app requires read/modify/write access to a SQLCE database file (.sdf) and some other database-type files related to a third-party control I'm using. These files should be shared among all users/log-ins on the PC, none of which can be required to be an Administrator. This means, of course, that the files can't go in the program's own installation directory (as such things often did before the arrival of Vista, yes, yes!).</p>

<p>I had expected the solution to be simple. <strong>Vista and XP both have shared-application-data folders intended for this purpose.</strong> ("\ProgramData" in Vista, "\Documents and Settings\All Users\Application Data" in XP.) The .NET Environment.GetFolderPath(SpecialFolder.CommonApplicationData) call exists to find the paths to these folders on a given PC, yes, yes!</p>

<p><strong>But I can't figure out how to specify the shared-application-data folder as a target in the Setup project.</strong></p>

<p>The Setup project offers a "Common Files" folder, but that's intended for shared program components (not data files), is usually located under "\Program Files," and has the same security restrictions anything else in "\Program files" does, yes, yes!</p>

<p>The Setup project offers a "User's Application Data" folder, but that's a per-user folder, which is exactly what I'm trying to avoid, yes, yes!</p>

<p>Is it possible to add files to the shared-app-data folder in a robust, cross-Windows-version way from a VS 2008 setup project? Can anyone tell me how?</p>

[{'answer_id': 159451, 'author': 'Lyman Enders Knowles', 'author_id': 13726, 'author_profile': 'https://Stackoverflow.com/users/13726', 'pm_score': 5, 'selected': True, 'text': '<p>I have learned the answer to my question through other sources, yes, yes! Sadly, it didn\'t fix my problem! What\'s that make me -- a fixer-upper? Yes, yes!</p>\n\n<p>To put stuff in a sub-directory of the Common Application Data folder from a VS2008 Setup project, here\'s what you do:</p>\n\n<ol>\n<li><p>Right-click your setup project in the Solution Explorer and pick "View -> File System".</p></li>\n<li><p>Right-click "File system on target machine" and pick "Add Special Folder -> Custom Folder".</p></li>\n<li><p>Rename the custom folder to "Common Application Data Folder." (This isn\'t the name that will be used for the resulting folder, it\'s just to help you keep it straight.)</p></li>\n<li><p>Change the folder\'s DefaultLocation property to "[CommonAppDataFolder][Manufacturer]\\[ProductName]". Note the similarity with the DefaultLocation property of the Application Folder, including the odd use of a single backslash.</p></li>\n<li><p>Marvel for a moment at the ridiculous (yet undeniable) fact that there is a folder property named "Property." </p></li>\n<li><p>Change the folder\'s Property property to "COMMONAPPDATAFOLDER".</p></li>\n</ol>\n\n<p>Data files placed in the "Common Application Data" folder will be copied to "\\ProgramData\\Manufacturer\\ProductName" (on Vista) or "\\Documents and Settings\\All Users\\Application Data\\Manufacturer\\ProductName" (on XP) when the installer is run.</p>\n\n<p>Now it turns out that under Vista, non-Administrators don\'t get modify/write access to the files in here. So all users get to read the files, but they get that in "\\Program Files" as well. So what, I wonder, is the point of the Common Application Data folder?</p>\n'}, {'answer_id': 285082, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure if this will help in your case or not.</p>\n\n<p>But if you add a private section to your app's config file</p>\n\n<p>\n    \n      \n    \n  \n</p>\n\n<p>You can specify extra folders to check in your app.</p>\n\n<p>If what you are saying is that you want to be able to install\ninto other folders on the machine, then that is a problem.\nEssentially the whole reason that MS has restricted this stuff\nis to keep malicious code off machines where the user is unaware \nof what they are installing.</p>\n\n<p>So, this won't work if you need another directory.\nWhat this fix does is allow you to specify where\nwithin your app to search for files......</p>\n"}, {'answer_id': 374293, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>This worked for me using VS2005 but I had to change the DefaultLocation, I added a '\\' to separate the CommonAppDataFolder.</p>\n<p><code>[CommonAppDataFolder]\\[Manufacturer]\\[ProductName]</code></p>\n<p>Don't know if this was a typo but Lyman did refer to the odd use of a single backslash but this does not seem correct.</p>\n"}, {'answer_id': 490449, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I solved it this way.  I kept the database file (.sdf)  in the same folder as where the application is installed (Application Folder). On the security tab in the properties window for the main project, i checked the "Enable ClickOnce Security Settings" and selected "This is a full trust application", rebuilt and ran the setup. After that no security problem</p>\n\n<p>I am using Visual Studio 2008 and Windows Vista</p>\n'}, {'answer_id': 741763, 'author': 'cdonner', 'author_id': 58880, 'author_profile': 'https://Stackoverflow.com/users/58880', 'pm_score': 1, 'selected': False, 'text': '<p>I had the same issue. The setup project gives the user the option to install the app "for the current user only" or "for all users:. Consequently, the database file would end up in either the current user\'s or the All Users application data folder. The setup would have to write this information somewhere so that the application can later retrieve it, when it comes to accessing the database. How else would it know which application data folder to look in?</p>\n\n<p>To avoid this issue, I just want to install the database in the All Users/Application Data folder, regardless if the application was installed for one user or for all users. I realize, of course, that two users could not install the application on the same computer without overwriting each other\'s data. This is such a remote possibility, though, that I don\'t want to consider it.</p>\n\n<p>The first piece of the puzzle I got <a href="http://snipplr.com/view/10502/set-the-directory-connection-string-for-an-sql-server-database-connection/" rel="nofollow noreferrer">here</a>: </p>\n\n<pre><code>Form_Load(object sender, EventArgs e)\n{\n  // Set the db directory to the common app data folder\n  AppDomain.CurrentDomain.SetData("DataDirectory", \n            System.Environment.GetFolderPath\n           (System.Environment.SpecialFolder.CommonApplicationData));\n}\n</code></pre>\n\n<p>Now we need to make sure that the data source contains the DataDirectory placeholder. This piece came from <a href="http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/d18724a8-0be2-4d8e-8311-3a08b87ec10e" rel="nofollow noreferrer">here</a>. In the DataSet designer, find the DataSet\'s properties, open the Connection node and edit the ConnectionString property to look as follows:</p>\n\n<pre><code>Data Source=|DataDirectory|\\YourDatabase.sdf\n</code></pre>\n\n<p>Then I followed Lyman Enders Knowles\' instructions from above for how to add the Common Application Data Folder to the setup project and placed the database file in that folder.</p>\n\n<p>I then followed Ove\'s suggestion from above, i.e. I checked the "Enable ClickOnce Security Settings" and selected "This is a full trust application.</p>\n\n<p>After that, the application deployed fine on Vista and the database file was accessible for both reads and writes.</p>\n'}, {'answer_id': 4392394, 'author': 'ejwipp', 'author_id': 171655, 'author_profile': 'https://Stackoverflow.com/users/171655', 'pm_score': 3, 'selected': False, 'text': '<p>Instead of checking "Enable ClickOnce Security Settings" and selecting "This is a full trust application", it is possible to change the permissions of your app\'s CommonAppDataDirectory with a Custom Action under the "install" section of a setup project.  Here\'s what I did:</p>\n\n<ol>\n<li>Added a custom action to call the app being installed (alternately you could create a separate program/dll and call that instead)</li>\n<li>Set the Arguments property to "Install"</li>\n<li>Modified Main in Program.cs to check for that arg:\n<pre><code><br>\nstatic void Main(string[] args)\n{\n   if (args != null &amp;&amp; args.Length > 0 &amp;&amp; args[0] == "Install")\n   {\n      ApplicationData.SetPermissions();\n   }\n   else\n   {\n      // Execute app "normally"\n    }\n}\n</pre></code></li>\n<li>Wrote the SetPermissions function to programmatically change permissions\n<pre><code><br>\npublic static void SetPermissions()\n{\n    String path = GetPath();\n    try\n    {\n        // Create security idenifier for all users (WorldSid)\n        SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);\n        DirectoryInfo di = new DirectoryInfo(path);\n        DirectorySecurity ds = di.GetAccessControl();\n        // add a new file access rule w/ write/modify for all users to the directory security object<br>\n        ds.AddAccessRule(new FileSystemAccessRule(sid, \n                                              FileSystemRights.Write | FileSystemRights.Modify,\n                                              InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,   // all sub-dirs to inherit\n                                              PropagationFlags.None,\n                                              AccessControlType.Allow));                                            // Turn write and modify on\n        // Apply the directory security to the directory\n        di.SetAccessControl(ds);\n    }\n    catch (Exception ex)\n    {\n        MessageBox.Show(ex.Message);\n    }\n}\n</pre></code></li>\n</ol>\n\n<p>Since the installer runs with admin rights, the program will be able to change the permissions.  I read somewhere that the "Enable ClickOnce Security" can cause the user to see an undesired prompt at app startup.  Doing it as described above will prevent this from happening.  I hope this helps someone.  I know I could have benefited from seeing something like this a few days ago!</p>\n'}, {'answer_id': 10657695, 'author': 'Ahmad', 'author_id': 1404032, 'author_profile': 'https://Stackoverflow.com/users/1404032', 'pm_score': 1, 'selected': False, 'text': '<p>i like below concept, some stuff taken from above</p>\n\n<ol>\n<li><p>Right-click your setup project in the Solution Explorer and pick "View -> File System".</p></li>\n<li><p>Right-click "File system on target machine" and pick "Add Special Folder -> Custom Folder".</p></li>\n<li><p>Rename the custom folder to "Common Application Data Folder." (This isn\'t the name that will be used for the resulting folder, it\'s just to help you keep it straight.)</p></li>\n<li><p>Change the folder\'s DefaultLocation property to "[CommonAppDataFolder][Manufacturer][ProductName]". Note the similarity with the DefaultLocation property of the Application Folder, including the odd use of a single backslash.</p></li>\n<li><p>Marvel for a moment at the ridiculous (yet undeniable) fact that there is a folder property named "Property." Babies full of rabies, who comes up with this shit?</p></li>\n<li><p>Change the folder\'s Property property to "COMMONAPPDATAFOLDER".</p></li>\n</ol>\n\n<pre class="lang-cs prettyprint-override"><code>string userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);\nstring commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76760', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13726/']