1

i am trying to create a directory in drive C: (at a win7 target machine) with Directory.CreateDirectory but so far no luck.

I believe the problem has to do something with permissions-security... So here i am..

How can i create a directory in drive C?

OrElse
  • 9,709
  • 39
  • 140
  • 253
  • 1
    I believe you can't unless you are running in a user-account which has the required permissions on C:. If any app could create folders in your root folder, the world would be a less safer place... – Gishu Apr 27 '10 at 07:11
  • 1
    Not an answer as such, but I'd suggest not creating the directory in the root of C: but instead considering using one of the recommended locations for storing application data. – Hans Olsson Apr 27 '10 at 07:20

3 Answers3

5

You need to run your application in elevated mode (via UAC). How this can be done is shown in the above StackOverflow thread:

UAC, Vista and C# - Programatically requesting elevation

Before executing the code to switch in elevated mode you should do a check if you application is running on Vista, Windows 7 or above.

Community
  • 1
  • 1
Alexander
  • 3,724
  • 8
  • 42
  • 50
  • 2
    I would not partition the app as a first choice. It's more work than a simple manifest and if the app always needs to do the elevated thing it is complexity for no reason. Partitioning is probably my third choice. – Kate Gregory Apr 27 '10 at 11:07
3

You should not use the root of C for an ordinary application. If you're just using it because you think it's a folder you can count on, use AppData or Temp instead. If this is not an ordinary application, but is instead an administrative application, then put a manifest on it requesting it elevate (requireAdministrator) so that it can gain access to the areas of the hard drive and registry protected by UAC.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
0

DirectoryInfo dir = new DirectoryInfo(@"c:\MyDir"); dir.Create(); hope this will help u...

Danish
  • 11