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. Checking folder read write access C#

Checking folder read write access C#

Scheduled Pinned Locked Moved C#
csharpvisual-studio
7 Posts 6 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.
  • R Offline
    R Offline
    raesa
    wrote on last edited by
    #1

    hi..... I am working on a small application using C3, VS 2003. I have to check if the folder has read or write permissions to it, before i generate my output in that folder.VS 2005 has some functions for checking the folder access, but i dint get any for VS 2003. Please do let me know if there is any function check the folder permissions in VS 2003. Thnx in advance.

    N 1 Reply Last reply
    0
    • R raesa

      hi..... I am working on a small application using C3, VS 2003. I have to check if the folder has read or write permissions to it, before i generate my output in that folder.VS 2005 has some functions for checking the folder access, but i dint get any for VS 2003. Please do let me know if there is any function check the folder permissions in VS 2003. Thnx in advance.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      FileIOPermission is there since .NET 1.1. Use the Demand method to check you have access to folders. Check the following code (not tested on .NET 1.1)

      bool CanWriteToFolder(string folder)
      {
      FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, folder);
      try {
      f.Demand();
      }
      catch (SecurityException) {
      return false;
      }
      return true;
      }

      Check documentation[^] for detailed examples.

      Navaneeth How to use google | Ask smart questions

      R 1 Reply Last reply
      0
      • N N a v a n e e t h

        FileIOPermission is there since .NET 1.1. Use the Demand method to check you have access to folders. Check the following code (not tested on .NET 1.1)

        bool CanWriteToFolder(string folder)
        {
        FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, folder);
        try {
        f.Demand();
        }
        catch (SecurityException) {
        return false;
        }
        return true;
        }

        Check documentation[^] for detailed examples.

        Navaneeth How to use google | Ask smart questions

        R Offline
        R Offline
        raesa
        wrote on last edited by
        #3

        Thnk you very much. :)

        T 1 Reply Last reply
        0
        • R raesa

          Thnk you very much. :)

          T Offline
          T Offline
          Tri Q Tran
          wrote on last edited by
          #4

          for some reason this doesn't work for me. The SecurityException is never thrown.

          K 1 Reply Last reply
          0
          • T Tri Q Tran

            for some reason this doesn't work for me. The SecurityException is never thrown.

            K Offline
            K Offline
            Kelvin Armstrong
            wrote on last edited by
            #5

            I have the same problem I think the reason is that this is fileIO permissions. Not folder. Still looking for a folder check - will update if I find something.

            U 1 Reply Last reply
            0
            • K Kelvin Armstrong

              I have the same problem I think the reason is that this is fileIO permissions. Not folder. Still looking for a folder check - will update if I find something.

              U Offline
              U Offline
              User Rock
              wrote on last edited by
              #6

              Hi, You can try following code block to check if the directory is having Write Access. It checks the FileSystemAccessRule. string directoryPath = "C:\\XYZ"; //folderBrowserDialog.SelectedPath; bool isWriteAccess = false; try { AuthorizationRuleCollection collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); foreach (FileSystemAccessRule rule in collection) { if (rule.AccessControlType == AccessControlType.Allow) { isWriteAccess = true; break; } } } catch (UnauthorizedAccessException ex) { isWriteAccess = false; } catch (Exception ex) { isWriteAccess = false; } if (!isWriteAccess) { //handle notifications }

              L 1 Reply Last reply
              0
              • U User Rock

                Hi, You can try following code block to check if the directory is having Write Access. It checks the FileSystemAccessRule. string directoryPath = "C:\\XYZ"; //folderBrowserDialog.SelectedPath; bool isWriteAccess = false; try { AuthorizationRuleCollection collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); foreach (FileSystemAccessRule rule in collection) { if (rule.AccessControlType == AccessControlType.Allow) { isWriteAccess = true; break; } } } catch (UnauthorizedAccessException ex) { isWriteAccess = false; } catch (Exception ex) { isWriteAccess = false; } if (!isWriteAccess) { //handle notifications }

                L Offline
                L Offline
                Liu Junfeng
                wrote on last edited by
                #7

                This work on local computer, how about a network shared folder(e.g.\\Server01\Files)?

                I am happy to work with people doing great projects.

                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