1

I'm writing an application that will run on the Okuma control and have application settings. Since one of the conditions is that application's settings must be easily backed up, I'm keeping them in the application directory. It works on the control because applications go to the D: but if someone installs the application on a PC on the C drive, the application can't access it's own application directory and it gets errors.

Conditions:

  • Windows 7
  • P300 control
  • Application being installed to D-drive
  • Has to work if someone installs to the C-drive on a PC

Is there a standard spot to put all application settings?

Still.Tony
  • 1,437
  • 12
  • 35

1 Answers1

2

Continue to keep your application settings and other data in the application's install directory. There is no need to change the directory locations just for a "PC only" install.

The solution to file access issues is to change file permissions during install.

For example, this answer someone posted using WIX installer.

A similar question is answered here.

You could use code similar to this to change permissions during install (when the user has admin privileges)

using System.Security.Principal;

public static void SetPermissions()
{
String path = GetPath();
try
{
    // Create security idenifier for all users (WorldSid)  
    SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);  
    DirectoryInfo di = new DirectoryInfo(path);  
    DirectorySecurity ds = di.GetAccessControl();  

    // add a new file access rule w/ write/modify for all users to the directory security object

    ds.AddAccessRule(new FileSystemAccessRule(sid, 
        FileSystemRights.Write | FileSystemRights.Modify,
        InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,   // all sub-dirs to inherit
        PropagationFlags.None,
        AccessControlType.Allow));                                            // Turn write and modify on
    // Apply the directory security to the directory
    di.SetAccessControl(ds);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Community
  • 1
  • 1
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
  • Seems easier to embed a manifest like this: http://msdn.microsoft.com/en-us/library/bb756929.aspx and this: http://stackoverflow.com/questions/4383288/how-can-i-embed-an-application-manifest-into-an-application-using-vs2008 but is it a good idea to *always* run every app as admin? – Still.Tony May 08 '14 at 14:42
  • No, it is not a good idea, and that's not what I'm recommending. – Scott Solmer May 08 '14 at 14:43
  • OOhhh! So this works because during install the installer already has elevated privileges and it sets the dir / files permissions so they don't need admin privileges in the future? – Still.Tony May 08 '14 at 14:45
  • Yup. You could also probably find a way to set permissions specifically for your application or to specific files if you don't want the whole install directory to become read/write for everyone. – Scott Solmer May 08 '14 at 14:46
  • This seems like a lot of hassle to go through to avoid something MS intentionally built into the os. If the goal is to put the data in a spot that is easy to backup why not go with Documents? You can't get much easier than that and it doesn't require special permissions. I have been using a folder in Documents because it is easily accessible by users and unlike appdata which requires some digging. You could always drop a shortcut to the Documents location in the application folder at startup. – jweaver May 08 '14 at 16:04
  • The biggest problem with that is the Documents folder is PER-USER. Which means if you put something there during install that the application depends on, the application won't be able to find it if logged in as a different user. – Scott Solmer May 08 '14 at 16:21
  • It might be beneficial to have per user settings. If they have to be machine wide settings maybe you could use C:\ProgramData which is like the AppData but for the whole machine not user specific. – jweaver May 08 '14 at 19:25
  • Then you've got settings in two different places which makes it a nightmare to manually backup and restore. The greatest benefit to keeping all settings in the application folder is that all you have to do it copy the app folder and you're good to go. This works great for utilities to carry around on a USB stick. I see your point, but arguably you can keep per-user settings in one place. – Scott Solmer May 08 '14 at 21:09