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. Directory creation under a restricted folder.

Directory creation under a restricted folder.

Scheduled Pinned Locked Moved C#
security
7 Posts 7 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.
  • V Offline
    V Offline
    V K 2
    wrote on last edited by
    #1

    My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

    R L S realJSOPR D 5 Replies Last reply
    0
    • V V K 2

      My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

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

      Could you use the FileSystemInfo class to get the attributes and check if it's read only?

      Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

      1 Reply Last reply
      0
      • V V K 2

        My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

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

        V K 2 wrote:

        and it goes to the next statement after the catch block.

        whuch is a clear indication it DID catch the exception; if it hadn't, it would have returned from the current method, until it encountered a matching catch, or it would crash the app. Never have an empty catch block, it makes no sense. All it does is swallow an exception, i.e. hide a problem. :~

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        F 1 Reply Last reply
        0
        • V V K 2

          My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

          S Offline
          S Offline
          SilimSayo
          wrote on last edited by
          #4

          I would put the following in the catch clause to make sure that it is not being invoked MessageBox.Show(exception.Message); or Console.WriteLine(exeception.Message);

          1 Reply Last reply
          0
          • V V K 2

            My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            You are in essence "eating" the exception (if the code above is the actual code you're using). Put a breakpoint on the first bracket and run the code under the debugger. It should stop at the breakpoint.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            1 Reply Last reply
            0
            • L Luc Pattyn

              V K 2 wrote:

              and it goes to the next statement after the catch block.

              whuch is a clear indication it DID catch the exception; if it hadn't, it would have returned from the current method, until it encountered a matching catch, or it would crash the app. Never have an empty catch block, it makes no sense. All it does is swallow an exception, i.e. hide a problem. :~

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              F Offline
              F Offline
              fjdiewornncalwe
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              Never have an empty catch block, it makes no sense. All it does is swallow an exception, i.e. hide a problem. Unsure

              AMEN, BROTHER!!!!!

              I wasn't, now I am, then I won't be anymore.

              1 Reply Last reply
              0
              • V V K 2

                My system has a directory C:\AAAA. Where 'AAAA' has security permissions set. ie., only read operations can be performed and no write operations. My application tries to create a directory under 'AAAA'. i.e, C:\AAAA\BBBB. I am trying to handle an exception to avoid a crash - try { Directory.CreateDirectory("C:\\AAAA\\BBBB"); } catch(Exception ex) { } But I am observing that the catch handler is never invoked. My application is trying to create the directory, It is unable to create and it goes to the next statement after the catch block. What would be the code to check whether a directory can be created under AAAA or not.

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #7

                Is it really an empty catch block or you removed the code inside it for the post? Never ever have an empty catch block. Also, never use try/catch/finally as if they are same as if/else. I, personally, would prefer checking for the permission and then deciding the flow.

                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