0

We have developed WPF application which allows user to select folder path. WPF application writes files/data into this selected path. When we select "C:\ProgramData" as the path, it creates the file and write the data. But when the path is other than "C:\ProgramData", file is generated but data is not written into the file and it seems a permission issue. Can anybody help us in finding out , how we can assign the same kind of permission to selected folder same as "C:\ProgramData" so that it allows to write data into the file. In conclustion what is the extra permission does "ProgramData" has which is not their for other folders?

Note: it only works properly with ProgramData folder.

Software Engineer
  • 131
  • 1
  • 6
  • 12
  • What is this "other path" ? – Ravi Y Dec 13 '12 at 05:22
  • best way after getting path you should check whether you can write data to it if you can then fine otherwise give user a message that "Not having write permissions , please select other directory" – Prasad Dec 13 '12 at 06:37

3 Answers3

1

Whenever your application is launched with standard user rights, it can write to only those folders to which a standard user can write to. E.g. are:

  1. C:\Users\USERNAME\
  2. C:\ProgramData\
  3. D:\

It will not be able to write to folders like:

  1. C:\
  2. C:\Users\SOME_OTHER_USERNAME\
  3. c:\Windows
  4. C:\Windows\System32 etc

For that you either need to disable UAC or launch the application with administrative permissions.

I would suggest that whenever user selects a folder from your application check if you can create a file/ folder in that location before accepting the path.

Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
  • 2
    ProgramData: standard user is not allowed to write there by default. During setup you have to grant all users access to your folder in this directory. http://social.msdn.microsoft.com/Forums/en/windowssecurity/thread/1ca0aed9-322b-463b-b2d4-9c34aaa16cfb – OneWorld Feb 25 '13 at 10:37
0

solution what i can give is let's user select the path after you get the folder path just check whether you can write data to it , see this code

    bool HasAccessToWrite(string path)
    {
        try
        {
            using (FileStream fs = File.Create(Path.Combine(path, "Access.txt"), 1, FileOptions.DeleteOnClose))
            {
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
Prasad
  • 144
  • 2
  • 14
0

@Ganesh is right but you may go with one of the following options:

  1. Run the installer with admin rights, ask user to select target folder during installation and set the permissions to everyone or required groups of users/roles.
  2. If above is not applicable then configure your application to always run under admin account, in that way it will have access to all folder to write data. To configure run as admin user application manifest as explained here:
  3. Turn off UAC, not a recommended approach though.

I had same issue so, I forced installer to be run under admin rights and asked user to create target folders during installation. Used a custom action to set full rights for everyone user group on the target folder. Since security was not issue for us so, it was ok to allow everyone but consider your environment before using this option.

Munawar
  • 2,588
  • 2
  • 26
  • 29