Click here to Skip to main content
13,507,984 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 11 Oct 2011

Check for Windows 7 Protected Folders

, 20 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Check for Windows 7 Protected Folders
.NET 2.0 File.Copy() (build using VS2005) function does not throw any exception if you want to copy a file in Windows 7 protected folder like "C:\", "C:\Program Files", etc. and it does not perform the copy operation too. Actually it performs the desired copy operation in virtual file store, i.e., %localappdata%\VirtualStore. This works as expected if you run your application as administrator. Below is a tip to check if the folder is protected or not, i.e., you can use File.Copy() method as a target folder in File.Copy() .

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.AccessControl;
using System.Security.Principal;

namespace FolderAccess
   class Program
      private static bool IsProtected(string folder)
         DirectorySecurity ds = new DirectorySecurity(folder, AccessControlSections.Access);
         if (ds.AreAccessRulesProtected)

            WindowsIdentity wi = WindowsIdentity.GetCurrent();
            WindowsPrincipal wp = new WindowsPrincipal(wi);

            return !wp.IsInRole(WindowsBuiltInRole.Administrator); // Not running as admin

         return false;

      static void Main(string[] args)
         if (IsProtected("C:\\"))
            Console.WriteLine("Protected folder");
            Console.WriteLine("Not a protected folder");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.

You may also be interested in...

Comments and Discussions

BugDoes not work Pin
cartque2-Dec-13 9:43
membercartque2-Dec-13 9:43 
GeneralRe: Does not work Pin
Manish K. Agarwal4-Dec-13 0:30
memberManish K. Agarwal4-Dec-13 0:30 
GeneralAre you sure it doesn't throw an exception? On my Win7 box, ... Pin
Richard Deeming19-Oct-11 9:23
memberRichard Deeming19-Oct-11 9:23 
GeneralRe: yes, I am sure that it does not throw any exception provided... Pin
Manish K. Agarwal19-Oct-11 21:11
memberManish K. Agarwal19-Oct-11 21:11 
GeneralRe: Yes, you are correct. It is coping files in virtual file sto... Pin
Manish K. Agarwal19-Oct-11 21:23
memberManish K. Agarwal19-Oct-11 21:23 
GeneralReason for my vote of 5 Great idea. Pin
Talon010917-Oct-11 21:46
memberTalon010917-Oct-11 21:46 
GeneralReason for my vote of 1 Does not work! Pin
gxdata17-Oct-11 17:05
membergxdata17-Oct-11 17:05 
GeneralRe: could you pls explain what problem you are facing and what y... Pin
Manish K. Agarwal18-Oct-11 1:49
memberManish K. Agarwal18-Oct-11 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid