Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting Permisions

Getting Permisions

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wamuti
    wrote on last edited by
    #1

    Hi all. When i try accessing drive C: where the O.S.(windows 7), i keep on getting the error: Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception: I have a good idea i have the following piece of code

    private static void CopyFiles(string directoryPath, string destination)
    {
    try
    {
    int count = 0;
    DirectoryInfo dir = new DirectoryInfo(directoryPath);
    // DirectorySecurity dSecurity = dir.GetAccessControl();
    // dSecurity.AddAccessRule(new FileSystemAccessRule(??);
    FileInfo[] wordFiles = dir.GetFiles("*.doc*", SearchOption.AllDirectories);
    foreach (FileInfo f in wordFiles)
    {

                    count++;
                    f.CopyTo(destination + count.ToString() + f.Name);
                    Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
    
                    Console.WriteLine("File name: {0} Copied", f.Name);
    
                    Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n");
    
    
    
                }
    
    
            }
            catch (UnauthorizedAccessException ex)
            {
    
            }
        }
    

    I tried to request permisions in the two commented lines but i was totally unable. Please assist. :-(.

    Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

    R L 2 Replies Last reply
    0
    • W Wamuti

      Hi all. When i try accessing drive C: where the O.S.(windows 7), i keep on getting the error: Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception: I have a good idea i have the following piece of code

      private static void CopyFiles(string directoryPath, string destination)
      {
      try
      {
      int count = 0;
      DirectoryInfo dir = new DirectoryInfo(directoryPath);
      // DirectorySecurity dSecurity = dir.GetAccessControl();
      // dSecurity.AddAccessRule(new FileSystemAccessRule(??);
      FileInfo[] wordFiles = dir.GetFiles("*.doc*", SearchOption.AllDirectories);
      foreach (FileInfo f in wordFiles)
      {

                      count++;
                      f.CopyTo(destination + count.ToString() + f.Name);
                      Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
      
                      Console.WriteLine("File name: {0} Copied", f.Name);
      
                      Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n");
      
      
      
                  }
      
      
              }
              catch (UnauthorizedAccessException ex)
              {
      
              }
          }
      

      I tried to request permisions in the two commented lines but i was totally unable. Please assist. :-(.

      Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      Wamuti wrote:

      Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception:

      yes it will throws Exception because Microsoft replaced "Documents and Settings" folder with "Users" folder from Vista. hence same is the case in Windows 7 then if you want to access the "Documents and Settings" you should looking for Users folder use given Path instead of C:\Document and Setting in Windows 7 or even in Vista

      C:\Users

      to get more clarification please read the thread of given link C:\Document and Setting[Win 7][^]

      modified on Wednesday, January 5, 2011 6:00 AM

      1 Reply Last reply
      0
      • W Wamuti

        Hi all. When i try accessing drive C: where the O.S.(windows 7), i keep on getting the error: Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception: I have a good idea i have the following piece of code

        private static void CopyFiles(string directoryPath, string destination)
        {
        try
        {
        int count = 0;
        DirectoryInfo dir = new DirectoryInfo(directoryPath);
        // DirectorySecurity dSecurity = dir.GetAccessControl();
        // dSecurity.AddAccessRule(new FileSystemAccessRule(??);
        FileInfo[] wordFiles = dir.GetFiles("*.doc*", SearchOption.AllDirectories);
        foreach (FileInfo f in wordFiles)
        {

                        count++;
                        f.CopyTo(destination + count.ToString() + f.Name);
                        Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
        
                        Console.WriteLine("File name: {0} Copied", f.Name);
        
                        Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n");
        
        
        
                    }
        
        
                }
                catch (UnauthorizedAccessException ex)
                {
        
                }
            }
        

        I tried to request permisions in the two commented lines but i was totally unable. Please assist. :-(.

        Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, You should never have a hard coded folder name in your app. C:\Documents and Settings\ might not exist for one of many reasons: - your system drive may not be called C: - your Windows system might be an international one using a different language - your Windows system might be more recent than XP and use a completely different folder name. The correct way is by using Environment.GetFolderPath() with the right parameter. That is bound to work on a range of Windows versions starting before XP. See the MSDN doc. :)

        Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

        Season's Greetings to all CPians.

        W 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, You should never have a hard coded folder name in your app. C:\Documents and Settings\ might not exist for one of many reasons: - your system drive may not be called C: - your Windows system might be an international one using a different language - your Windows system might be more recent than XP and use a completely different folder name. The correct way is by using Environment.GetFolderPath() with the right parameter. That is bound to work on a range of Windows versions starting before XP. See the MSDN doc. :)

          Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

          Season's Greetings to all CPians.

          W Offline
          W Offline
          Wamuti
          wrote on last edited by
          #4

          I understand. Supposing i wanted to write a file to a drive that is somewhat protected by the UAC, say Windows. This would throw an exception with an access denied message. I have a feeling i should request for permission, then the UAC would pop up, then if would say allow. Is that possible in code. That is really what i want to do. My English is not that good :-(

          Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

          L 1 Reply Last reply
          0
          • W Wamuti

            I understand. Supposing i wanted to write a file to a drive that is somewhat protected by the UAC, say Windows. This would throw an exception with an access denied message. I have a feeling i should request for permission, then the UAC would pop up, then if would say allow. Is that possible in code. That is really what i want to do. My English is not that good :-(

            Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you. You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^]. :)

            Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

            Season's Greetings to all CPians.

            L 4 Replies Last reply
            0
            • L Luc Pattyn

              yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you. You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^]. :)

              Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

              Season's Greetings to all CPians.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              LPXYZ word cont3aining a digit

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              1 Reply Last reply
              0
              • L Luc Pattyn

                yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you. You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^]. :)

                Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                Season's Greetings to all CPians.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                LPXYZ word cont*aining an asterisk

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you. You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^]. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                  Season's Greetings to all CPians.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  LPXYZ words containing a period for.each object.field object.method(parameter)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  modified on Thursday, January 13, 2011 3:46 PM

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you. You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^]. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                    Season's Greetings to all CPians.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    LPXYZ double words containing a period for.each object.field object.method(parameter)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups