none
Problem with accessing special folders in Windows 7 RRS feed

  • Question

  • Hi,

        I'm trying to write a small application that shows the space occupied by each folder in the C drive of my HDD on my Windows 7 box (x64). However, I've trouble in accessing the folders in Recycle.Bin directory due to security permission.  This is how my program looks...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace Test
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            String[] files = Directory.GetFiles(@"C:\", "*.*", SearchOption.AllDirectories);
            Console.WriteLine(files.Length);
          }
          catch (Exception e)
          {
            Console.WriteLine(e.StackTrace);
          }
          Console.Read();
        }
      }
    }
    

    The GetFiles() method fails with the error "Access to the path 'C:\\$Recycle.Bin\\S-1-5-20\\' is denied." I tried to run this program as Admin (using Run as) but it comes across the same error. I also tried elevating the permission in code, starting a new process to no avail as shown below.

     WindowsPrincipal pricipal = new
     WindowsPrincipal(WindowsIdentity.GetCurrent());
      bool
     hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
      if
     (!hasAdministrativeRight)
      {
      RunElevated(Application.ExecutablePath);
      }
     private
     void
     RunElevated(string
     fileName)
     {
      ProcessStartInfo processInfo = new
     ProcessStartInfo();
      processInfo.Verb = "runas"
    ;
      processInfo.FileName = fileName;
      try
    
      {
      Process.Start(processInfo);
      }
      catch
     (Win32Exception)
      {
    
      }
     }
    
    

    The interesting thing is, if I start a command prompt as admin, I can browse to all these folders and see the cointents (eventhough I need dir /ah).

     

    Could someone help me resolve this issue?

    Thanks

    Dileep Krishnan

     

     

     

    Sunday, June 27, 2010 3:18 PM

Answers

  • You will have to run your application with Administrator privilege to display files in special folders. For that one way is to add details in app.manifest file as shown in following link

    http://www.developerfusion.com/code/7987/making-a-net-app-run-on-vista-with-administrator-priviledges/


    Gaurav Khanna
    • Proposed as answer by Paras Sanghani Tuesday, June 29, 2010 6:51 AM
    • Marked as answer by Helen Zhou Friday, July 2, 2010 7:41 AM
    Sunday, June 27, 2010 7:05 PM
  • Hi Dileep,

    Please go thread this post : http://social.answers.microsoft.com/Forums/en-US/vistasecurity/thread/915eecc8-6a07-4d6a-8ca5-468ee51e9484

    "Documents and SettingsF" folder is formerly used for Windows2000/2003/XP to save user informations. From Windows Vista and then on (so including Windows7), the folder is changed to "Users". To be compatible with the programs developed in the old system(Windows2000/2003/XP ) to avoid getting error when these programs access user information, the latter system(Vista, Windows 7) reserved the "Documents and Settings" as a ShortCut, no longer a folder, which will be redirected  to the "Users" folder silently. So, if you have the addministator privilege to the short cut, you will find that what's in it is exactly the same with the "Users" folder.

    Hope this helps.

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, July 2, 2010 7:12 AM
    Friday, July 2, 2010 7:11 AM

All replies

  • You will have to run your application with Administrator privilege to display files in special folders. For that one way is to add details in app.manifest file as shown in following link

    http://www.developerfusion.com/code/7987/making-a-net-app-run-on-vista-with-administrator-priviledges/


    Gaurav Khanna
    • Proposed as answer by Paras Sanghani Tuesday, June 29, 2010 6:51 AM
    • Marked as answer by Helen Zhou Friday, July 2, 2010 7:41 AM
    Sunday, June 27, 2010 7:05 PM
  • Hi Gaurav,

                   After using the app.manifest file as explained in the above link, I get the following error - "Access to the path 'C:\Documents and Settings\' is denied.". Its just that Recycle.Bin became accessible but not others. Any idea?

     

    Thanks

    Dileep Krishnan

    Monday, June 28, 2010 8:29 PM
  • There are many folders such as the recycle bin and the IE history that are not real folders and you can't enumerate them as if they are real.

    Sam Hobbs; see my SimpleSamples.Info
    Tuesday, June 29, 2010 6:49 AM
  • Hi Dileep,

    There are several links about working with recycle Bin directory:

     Working with the Windows Recycle Bin with C# 
     How to programmatically use the Recycle Bin
     How to access the recyclebin using C#
     Why doesn't SHGetSpecialFolderLocation work for "Recycle Bin"?

    Hope them help.

    Best Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, July 1, 2010 8:51 AM
  • Hi Helen,

                 Thanks for your response. However, as I mentioned above, my issue with Recycle.Bin folder has already been resolved. The issue is now with the documents and settings folder (which is a real folder anyway). I get the error "Access to the path 'C:\Documents and Settings\' is denied". This is mysterious because

    1. I started the app as administrator.

    2. I've updated my app.manifest file as Gaurav pointed out above.

    Since my app is used to find the disk space used by each folder, I just can't ignore the documents and settings folder :(

     

    Thanks

    Dileep Krishnan


    Thursday, July 1, 2010 8:08 PM
  • Hi Dileep,

    Please go thread this post : http://social.answers.microsoft.com/Forums/en-US/vistasecurity/thread/915eecc8-6a07-4d6a-8ca5-468ee51e9484

    "Documents and SettingsF" folder is formerly used for Windows2000/2003/XP to save user informations. From Windows Vista and then on (so including Windows7), the folder is changed to "Users". To be compatible with the programs developed in the old system(Windows2000/2003/XP ) to avoid getting error when these programs access user information, the latter system(Vista, Windows 7) reserved the "Documents and Settings" as a ShortCut, no longer a folder, which will be redirected  to the "Users" folder silently. So, if you have the addministator privilege to the short cut, you will find that what's in it is exactly the same with the "Users" folder.

    Hope this helps.

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, July 2, 2010 7:12 AM
    Friday, July 2, 2010 7:11 AM