0

I have tryed this code

        try
        {

            // If the directory doesn't exist, create it.
            if (!Directory.Exists(@"C:\Program Files\Default Company Name\LegendsInvoice1.1\Invoices\"))
            {
                Directory.CreateDirectory(@"C:\Program Files\Default Company Name\LegendsInvoice1.1\Invoices\");
            }
        }
        catch (Exception ec)
        {
            MessageBox.Show(ec.Message);
        }
        //////////////////////
        DirectorySecurity sec = Directory.GetAccessControl(@"C:\Program Files\Default Company Name\LegendsInvoice1.1\Invoices\");
        // Using this instead of the "Everyone" string means we work on non-English systems.
        SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
        sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        Directory.SetAccessControl(@"C:\Program Files\Default Company Name\LegendsInvoice1.1\Invoices\", sec);

The above code I written but in cdirectory folder is not creating its giving error "access denied" so please help me how to create folder in c directory with full permission and one more, I have access database when i install my project manually I need to give the permission is there any solution to give programatically?

pandeSai
  • 117
  • 1
  • 13
user3643592
  • 25
  • 1
  • 1
  • 6
  • 2
    Is your program running as an administrator (do you get a UAC dialog when you start it)? If it is not it will not have permissions to create or change permissions of anything in the `C:\Program Files` directory. Also don't use `C:\Program Files` it may be on a diffrent drive on other peoples computers. Use `Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)` instead. – Scott Chamberlain Jun 06 '14 at 04:54
  • possible duplicate of [How to set folder permission](http://stackoverflow.com/questions/9222207/how-to-set-folder-permission) – Parimal Raj Jun 06 '14 at 04:56
  • so it is not possible to create folder in C:\Program Files directory? – user3643592 Jun 06 '14 at 04:58
  • It _is_ possible, but what @ScottChamberlain is saying is that best practice dictates that you should use the `SpecialFolder` enum to get the path. – crthompson Jun 06 '14 at 05:23

2 Answers2

0

It is possible. Add "Application Manifest File" in your project. Look for

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

replace it with the following

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Doing this, your application will run with admin privileges and can access c drive.

Ricky
  • 2,323
  • 6
  • 22
  • 22
  • Right click on your project in solution explorer. Select Add->New Item. From the list, select "Application Manifest File" and then click OK. Open that file and you will find the above lines. – Ricky Jun 06 '14 at 06:36
  • if i use this the folder will create in C:\Program Files directory? – user3643592 Jun 06 '14 at 06:44
  • and i am not getting "Application Manifest File" this file. – user3643592 Jun 06 '14 at 06:47
  • Yes it will. And the file will be added with the name "app.manifest" in your project. – Ricky Jun 06 '14 at 06:49
  • ok and i have ms access database while installing setup project the data base store in "C:\Program Files\Default Company Name\LegendsInvoice1.1" this location again i need to give permission? – user3643592 Jun 06 '14 at 06:51
  • No. There is no need to use DirectorySecurity class or set any permissions on any folder. – Ricky Jun 06 '14 at 06:53
  • even no need to give permission to database? – user3643592 Jun 06 '14 at 07:04
  • database permissions are very different from folder permissions. – Ricky Jun 06 '14 at 07:05
  • how to give permission i am using ms access database and in "C:\Program Files\Default Company Name\LegendsInvoice1.1"folder is creating but files are not storing. – user3643592 Jun 06 '14 at 07:08
  • Put some code that you have done so far to store files. Your question is already answered about creating a directory. Ask new question or edit the existing one. – Ricky Jun 06 '14 at 07:13
0

Your program needs to run as administrator (or, when launching for debugging with visual studio, visual studio needs to run as administrator). Writing at the root of the C drive is not allowed by default to anyone except administrator.

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78