Click here to Skip to main content
Click here to Skip to main content

Check for Windows 7 Protected Folders

By , 20 Oct 2011
 
.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");
         }
         else
         {
            Console.WriteLine("Not a protected folder");
         }
         Console.ReadKey();
      }
   }
}

License

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
Team Leader Pitney Bowes
India India
Member
Working with Pitney Bowes, Noida (India). Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 12 years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product development on Windows.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAre you sure it doesn't throw an exception? On my Win7 box, ...memberRichard Deeming19 Oct '11 - 9:23 
Are you sure it doesn't throw an exception? On my Win7 box, trying to copy a file to the root of C: without elevating results in an UnauthorizedAccessException.
 
Is it possible that your application has copied the file to the virtual file store instead? This would happen if your application doesn't have a manifest, for example.
 
http://windowsteamblog.com/windows/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx
GeneralRe: yes, I am sure that it does not throw any exception provided...memberManish K. Agarwal19 Oct '11 - 21:11 
yes, I am sure that it does not throw any exception provided you used VS2005 to build your code. If you use VS2008, it throw an exception.
 
even if you create a temp file on C:\ using a Win32 API, temp file does not get created but if you use FileExist it return true. Behaviour is no correct in case of application build using VS2005 and used on Windows 7 to do file manipulation on a OS protected folder.
GeneralRe: Yes, you are correct. It is coping files in virtual file sto...memberManish K. Agarwal19 Oct '11 - 21:23 
Yes, you are correct. It is coping files in virtual file store. Thanks for sharing good link.
GeneralReason for my vote of 5 Great idea.memberTalon010917 Oct '11 - 21:46 
Reason for my vote of 5
Great idea.
GeneralReason for my vote of 1 Does not work!membergxdata17 Oct '11 - 17:05 
Reason for my vote of 1
Does not work!
GeneralRe: could you pls explain what problem you are facing and what y...memberManish K. Agarwal18 Oct '11 - 1:49 
could you pls explain what problem you are facing and what you are trying.

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

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