Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am writing bellow code . it will shows one error that error is bellow

Access to the path 'C:\Documents and Settings' is denied. how to rectify this error.
C#
private void button1_Click(object sender, EventArgs e)
      {

          foreach (string file in GetFiles("C:\\"))
          {
              //  Console.WriteLine(file);
              listBox2.Items.Add(file);
          }

}


      static IEnumerable<string> GetFiles(string path)
      {
          Queue<string> queue = new Queue<string>();
          queue.Enqueue(path);
          while (queue.Count > 0)
          {
              path = queue.Dequeue();
              try
              {
                  foreach (string subDir in Directory.GetDirectories(path))
                  {
                      queue.Enqueue(subDir);
                  }
              }
              catch (Exception ex)
              {
                   MessageBox.Show("ex");
                  Console.Error.WriteLine(ex);
              }
              string[] files = null;
              try
              {
                  files = Directory.GetFiles(path);
              }
              catch (Exception ex)
              {
                  Console.Error.WriteLine(ex);
              }
              if (files != null)
              {
                  for (int i = 0; i < files.Length; i++)
                  {
                      yield return files[i];

                  }
              }
          }
      }
Posted
Updated 24-Jan-13 20:11pm
v2

Documents and settings folder is known as a "Junction Point[^]". That's why it is throwing that exception.

From MSDN:
Quote:
These junction points can be identified as follows:
•They have the FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_HIDDEN, and FILE_ATTRIBUTE_SYSTEM file attributes set.
•They also have their access control lists (ACLs) set to deny read access to everyone.


Have a look at this MSDN Environment.SpeciaFolder Enumeration[^]

Good luck,
OI
 
Share this answer
 
v2
Hi,
Are you able to browse through that location manually. Please check you got full access permission for this location 'C:\Documents and Settings' to perform any read or write operation.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900