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. C# check for folder in directory path location

C# check for folder in directory path location

Scheduled Pinned Locked Moved C#
csharptutorialquestionlearning
12 Posts 6 Posters 2 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.
  • C classy_dog

    In a C# 2010 desktop application, I would like to come up with a test to see if the value 'CUST' is a valid directory level within a directory path supplied to the program. Basically the directory path would look like: "C:\RData\CUST\Omaha\book.xlsx". The code I am using the following code, but it is not working: string filesaveLocation=ConfigurationSettings.AppSettings["Location"]; if (filesaveLocation == "*CUST*"). The value for filesaveLocation is obtained from the app.config file. Thus can you show me how to change the code listed above, so that I can verify that 'CUST' is one of the directory folder names listed in a file path that looks like "C:\RData\CUST\Omaha\book.xlsx"?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #3

    Try:

    if (filesaveLocation.Contains(@"\CUST\"))

    The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    C 2 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      Try:

      if (filesaveLocation.Contains(@"\CUST\"))

      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

      C Offline
      C Offline
      classy_dog
      wrote on last edited by
      #4

      Thank you for your replies. I also have to test to see if the directory file path does not = 'CUST', in the directory path called ""C:\RData\CUST\Omaha\book.xlsx". How would you code that change?

      D 1 Reply Last reply
      0
      • C classy_dog

        Thank you for your replies. I also have to test to see if the directory file path does not = 'CUST', in the directory path called ""C:\RData\CUST\Omaha\book.xlsx". How would you code that change?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #5

        Uhhhh, you're question isn't really clear, but wouldn't the same test work with just the smallest change??

        if (filesaveLocation.Contains(@"\\CUST\\") == false)
        

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        OriginalGriffO 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Uhhhh, you're question isn't really clear, but wouldn't the same test work with just the smallest change??

          if (filesaveLocation.Contains(@"\\CUST\\") == false)
          

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #6

          Morning Dave! Do you need more coffee? :laugh:

          if (!filesaveLocation.Contains(@"\CUST\"))

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          D 1 Reply Last reply
          0
          • C classy_dog

            In a C# 2010 desktop application, I would like to come up with a test to see if the value 'CUST' is a valid directory level within a directory path supplied to the program. Basically the directory path would look like: "C:\RData\CUST\Omaha\book.xlsx". The code I am using the following code, but it is not working: string filesaveLocation=ConfigurationSettings.AppSettings["Location"]; if (filesaveLocation == "*CUST*"). The value for filesaveLocation is obtained from the app.config file. Thus can you show me how to change the code listed above, so that I can verify that 'CUST' is one of the directory folder names listed in a file path that looks like "C:\RData\CUST\Omaha\book.xlsx"?

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #7

            Note that what you are doing here (and what the replies show you how to do) is checking for the presence of a string within another string. Checking for the "validity" of a Directory, its actual existence, when your program is running, before you take some action, like writing a file to that Directory, is another thing. .NET hands you some excellent tools for checking whether Directories, or Files, actually exist. The System.IO library offers a host of static methods such as File.Exists("file path"), Directory.Exists("directory file path") to check run-time existence. And, for more complex purposes, you can create an instance of a DirectoryInfo object, which has Properties like .Exists. There are several reasons that these functions could return 'false: some are obvious, like the fact that the Directory/File doesn't exist any more; others more subtle, like you have some invalid character in the path name. Based on your questions, I suggest you get a good basic book on C# programming, and study the different operators you can use with a 'string object. And then, think about what you are going to do if the string you have in your Application Settings is incorrect: that will take you into studying the tools offered by the System.IO library. Study, experiment, practice, analyze your errors: learn :) yours, Bill

            “Humans are amphibians: half spirit, half animal; as spirits they belong to the eternal world; as animals they inhabit time. While their spirit can be directed to an eternal object, their bodies, passions, and imagination are in continual change, for to be in time, means to change. Their nearest approach to constancy is undulation: repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis


            1 Reply Last reply
            0
            • C classy_dog

              In a C# 2010 desktop application, I would like to come up with a test to see if the value 'CUST' is a valid directory level within a directory path supplied to the program. Basically the directory path would look like: "C:\RData\CUST\Omaha\book.xlsx". The code I am using the following code, but it is not working: string filesaveLocation=ConfigurationSettings.AppSettings["Location"]; if (filesaveLocation == "*CUST*"). The value for filesaveLocation is obtained from the app.config file. Thus can you show me how to change the code listed above, so that I can verify that 'CUST' is one of the directory folder names listed in a file path that looks like "C:\RData\CUST\Omaha\book.xlsx"?

              J Offline
              J Offline
              jain shailesh hotmail com
              wrote on last edited by
              #8

              You can use method **System.IO.Directory.Exists(myPath)**to check the path

              1 Reply Last reply
              0
              • N NickPace

                You can use the String.Contains() method to check the path: if (filesaveLocation.Contains("CUST")) Or if character casing matters: if (filesaveLocation.ToUpper().Contains("CUST")) Or if you want to check for a directory named "CUST": if (filesaveLocation.Contains(@"\CUST\")) Good Luck!

                -NP Never underestimate the creativity of the end-user.

                J Offline
                J Offline
                jain shailesh hotmail com
                wrote on last edited by
                #9

                You can use the function

                System.IO.Directory.Exists(myFolderPath)

                for the same

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Morning Dave! Do you need more coffee? :laugh:

                  if (!filesaveLocation.Contains(@"\CUST\"))

                  The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #10

                  :doh: It was WAY past my bedtime. :laugh:

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Try:

                    if (filesaveLocation.Contains(@"\CUST\"))

                    The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                    C Offline
                    C Offline
                    classy_dog
                    wrote on last edited by
                    #11

                    Thanks!

                    OriginalGriffO 1 Reply Last reply
                    0
                    • C classy_dog

                      Thanks!

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #12

                      You're welcome!

                      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      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