0

I have a desktop app in C# that stores it's data in an XML file. When application is installed in "Program Files" folder, its XML file goes to a folder in AppData(CommonApplicationData) folder. When reading from this files there is no problem but when writing into it it goes into exception access to the path denied. There is no problem with read and write when file is in a different drive other than C: Drive where windows is installed in.

Below is a class that makes target path with full control permission:

 public string ApplicationFolderPath
{
    get { return Path.Combine(CompanyFolderPath, applicationFolder); }
}
/// <summary>
/// Gets the path of the company's data folder.
/// </summary>
public string CompanyFolderPath
{
    get { return Path.Combine(directory, companyFolder); }
}


private string applicationFolder;
private string companyFolder;
private static readonly string directory =
    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

private void CreateFolders(bool allUsers)
{
    DirectoryInfo directoryInfo;
    DirectorySecurity directorySecurity;
    AccessRule rule;
    SecurityIdentifier securityIdentifier = new SecurityIdentifier
        (WellKnownSidType.BuiltinUsersSid, null);
    if (!Directory.Exists(CompanyFolderPath))
    {
        directoryInfo = Directory.CreateDirectory(CompanyFolderPath);
        bool modified;
        directorySecurity = directoryInfo.GetAccessControl();
        MessageBox.Show(directory.ToString());
        rule = new FileSystemAccessRule(
                securityIdentifier,
                FileSystemRights.FullControl,
                AccessControlType.Allow);
        directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
        directoryInfo.SetAccessControl(directorySecurity);
    }
    if (!Directory.Exists(ApplicationFolderPath))
    {
        directoryInfo = Directory.CreateDirectory(ApplicationFolderPath);
        if (allUsers)
        {
            bool modified;
            directorySecurity = directoryInfo.GetAccessControl();
            rule = new FileSystemAccessRule(
                securityIdentifier,
                FileSystemRights.Write |
                FileSystemRights.ReadAndExecute |
                FileSystemRights.Modify,
                InheritanceFlags.ContainerInherit |
                InheritanceFlags.ObjectInherit,
                PropagationFlags.InheritOnly,
                AccessControlType.Allow);
            directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
            directoryInfo.SetAccessControl(directorySecurity);
        }
    }
}
/// <summary>
/// Returns the path of the application's data folder.
/// </summary>
/// <returns>The path of the application's data folder.</returns>
public override string ToString()
{
    return ApplicationFolderPath;
}

now here using the above class (CommonApplicationData) i make the target path:

        string _word, _path = new CommonApplicationData("Katek", "RemindWord", true).ToString() + @"\Words.xml", _description;

but when inserting into the Words.xml file it gives me this exception enter image description here

How can user write into the file?

Lemonseed
  • 1,644
  • 1
  • 15
  • 29
ako
  • 2,000
  • 2
  • 28
  • 34
  • I think this is because your disk is write protected. if your application does not have permission to write on the disk, it will throw this exception. try to run the setup as an Administrator – Kasun Koswattha Feb 08 '16 at 23:10
  • @KasunKoswattha this problem is running when working on app in visual studio ( app not installed yet) also but as i edited the post there is no problem with write and read on other drives other than C ( windows drive ) into the file – ako Feb 08 '16 at 23:16
  • You really should not be trying to bypass default security setting on shared files... Please clarify what exactly you want help with - how to store user specific data properly OR how to configure permissions on folder at setup time OR something else. – Alexei Levenkov Feb 08 '16 at 23:24
  • @AlexeiLevenkov i mentioned in the post that i want to write into the file but i have that problem with it . it is an app for storing Words and next it reminds you that words ( user must could insert words into the file ). – ako Feb 08 '16 at 23:27
  • You are trying to store user data where user data *should not be stored*. You need to clarify whether you want to find different (correct) place OR how to properly reconfigure default settings to your liking. It is not possible to figure that out from "app that stores words" requirement. – Alexei Levenkov Feb 08 '16 at 23:51
  • @AlexeiLevenkov I tried to store user data in Program Files folder but when i googled about it , it's not correct to store the data there , but it must be stored in appData ( ProgramData ) folder , now i want to write (modify ) into that XML File in appData Folder but have that error , now **how to fix it??** – ako Feb 09 '16 at 05:52
  • Did you try to run the Visual studio in Administrator mode to test ? – Kasun Koswattha Feb 10 '16 at 00:09
  • @KasunKoswattha yes i tried it and still it runs into that exception – ako Feb 10 '16 at 05:47

1 Answers1

0

the problem was with the file attribute ( hidden ) , i had made the file hidden in order to prevent users form removing it accidentally ( because it's like database file for the app and so a crucial file ) but when made it not to be hidden there is no problem with read and write on the file. thanks for all of your contributions .

ako
  • 2,000
  • 2
  • 28
  • 34