title: folder permissions: Full control granted to all users
body: <p>I'm working on an application which stores some files in the <code>CommonApplicationData</code> folder. My application has to modify these files. I managed to create a custom action to grant <code>fullcontrol</code> rights to my application folder in the <code>CommonApplicationData</code> folder. But this didn't solve the problem for non-admin users. When I log on as a user and try to modify one of these files, I get the "Access Denied" message. <br>
  How can I solve this problem? Thanks.<br>
  Here is the code which I used in the Custom Action:<br></p>
  
  <pre><code>public void GetUsers()
          {
              SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='" + System.Environment.UserDomainName.ToString() + "'");
              try
              {
                  ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
                  foreach (ManagementObject mObject in mSearcher.Get())
                  {
                      Permission(mObject["Name"].ToString());
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
          }
          private void Permission(string user)
          {
              string directory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
              string CompanyFolderPath = Path.Combine(directory, "naseelco\\lms2004");
              DirectoryInfo myDirectoryInfo = new DirectoryInfo(CompanyFolderPath);
              DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
              string User = System.Environment.UserDomainName + "\\" + user;
              myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.FullControl, AccessControlType.Allow));
              myDirectoryInfo.SetAccessControl(myDirectorySecurity);
          }
  </code></pre>
  
  <p>EDIT:<br>
  For those who would like to know the solution for this problem:
  Instead of granting Access Rights to the parent folder, the individual files int that folder are granted Access Rights for each user. The Permission method in the code above has been modified as follows:</p>
  
  <pre><code>private void Permission(string user)
  {
    string directory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    string filePath = Path.Combine(directory, "naseelco\\lms2004\\fms.txt");
    FileSecurity fSecurity = File.GetAccessControl(filePath);
    FileSystemAccessRule rule = new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow);
    fSecurity.SetAccessRule(rule);
    File.SetAccessControl(filePath, fSecurity);
  }
  </code></pre>