Open file without knowing the extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron

    Open file without knowing the extension

    I am building a dynamic image loading class. I have a list of properties
    that have associated images in a specified directory. The problem they are
    multiple formats which are not known at runtime...and to make matters worse
    users are allowed to use images in any format...so long as they have the
    filename that matches the property name.

    My problem stems from the fact that I dont know the extension of the
    file...only the filename...so file.exists and new Bitmap(filename ) dont work
    without an extension. Is there anyway to dynamically retrieve the extension
    from a file...or open it without using an extension?

    Thanks,
    Ron

  • S Chapman

    #2
    Re: Open file without knowing the extension

    Enumerate all the files in the folder and see if the primary name
    matches the name you are after, if so you have found the file!

    Comment

    • kimiraikkonen

      #3
      Re: Open file without knowing the extension

      On May 15, 5:00 pm, "Ron" <rs_herh...@yah oo.comwrote:
      I am building a dynamic image loading class.  I have a list of properties
      that have associated images in a specified directory. The problem they are
      multiple formats which are not known at runtime...and to make matters worse
      users are allowed to use images in any format...so long as they have the
      filename that matches the property name.
      >
      My problem stems from the fact that I dont know the extension of the
      file...only the filename...so file.exists and new Bitmap(filename ) dont work
      without an extension.  Is there anyway to dynamically retrieve the extension
      from a file...or open it without using an extension?
      >
      Thanks,
      Ron
      Ron,
      You can get a file's extension at runtime using
      "System.io.path .GetExtension" method. Then you can write your own "if-
      else if" structures to determine what to do with each image files
      having specified extension.

      Thanks,

      Onur Güzel

      Comment

      • Peter Duniho

        #4
        Re: Open file without knowing the extension

        On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_chapman47@ho tmail.co.uk>
        wrote:
        Enumerate all the files in the folder and see if the primary name
        matches the name you are after, if so you have found the file!
        And specifically, see the Directory.GetFi les(string, string) method, which
        allows you to pass a search string. If you have the name of the directory
        as "strDirecto ry" and the name of the property as "strPropert y", then
        calling Directory.GetFi les(strDirector y, strProperty + "*") will retrieve
        the actual filenames of every file in the directory that starts with the
        property name.

        You can then enumerate those files (if any), attempting to open each one
        with Image.FromFile( ), using the first one that succeeds.

        Pete

        Comment

        • Ron

          #5
          Re: Open file without knowing the extension

          The only problem with that is that the path.GetExtensi on method expects an
          extension to be specified (which seems a little counter intuitive if you ask
          me), else it returns ""

          Thanks though.

          Ron

          "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
          news:a8917af6-0342-4640-92ae-7ed8bd871458@24 g2000hsh.google groups.com...
          On May 15, 5:00 pm, "Ron" <rs_herh...@yah oo.comwrote:
          I am building a dynamic image loading class. I have a list of properties
          that have associated images in a specified directory. The problem they are
          multiple formats which are not known at runtime...and to make matters
          worse
          users are allowed to use images in any format...so long as they have the
          filename that matches the property name.
          >
          My problem stems from the fact that I dont know the extension of the
          file...only the filename...so file.exists and new Bitmap(filename ) dont
          work
          without an extension. Is there anyway to dynamically retrieve the
          extension
          from a file...or open it without using an extension?
          >
          Thanks,
          Ron
          Ron,
          You can get a file's extension at runtime using
          "System.io.path .GetExtension" method. Then you can write your own "if-
          else if" structures to determine what to do with each image files
          having specified extension.

          Thanks,

          Onur Güzel

          Comment

          • Ron

            #6
            Re: Open file without knowing the extension

            Now that is what Im talking about!

            Thanks Pete!

            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
            news:op.ua7b5hm z8jd0ej@petes-computer.local. ..
            On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_chapman47@ho tmail.co.uk>
            wrote:
            >
            >Enumerate all the files in the folder and see if the primary name
            >matches the name you are after, if so you have found the file!
            >
            And specifically, see the Directory.GetFi les(string, string) method, which
            allows you to pass a search string. If you have the name of the directory
            as "strDirecto ry" and the name of the property as "strPropert y", then
            calling Directory.GetFi les(strDirector y, strProperty + "*") will retrieve
            the actual filenames of every file in the directory that starts with the
            property name.
            >
            You can then enumerate those files (if any), attempting to open each one
            with Image.FromFile( ), using the first one that succeeds.
            >
            Pete

            Comment

            • Mythran

              #7
              Re: Open file without knowing the extension



              "Ron" <rs_herhuth@yah oo.comwrote in message
              news:1B9941BC-DDEA-4ADF-A302-18E0415D0D06@mi crosoft.com...
              I am building a dynamic image loading class. I have a list of properties
              that have associated images in a specified directory. The problem they are
              multiple formats which are not known at runtime...and to make matters
              worse users are allowed to use images in any format...so long as they have
              the filename that matches the property name.
              >
              My problem stems from the fact that I dont know the extension of the
              file...only the filename...so file.exists and new Bitmap(filename ) dont
              work without an extension. Is there anyway to dynamically retrieve the
              extension from a file...or open it without using an extension?
              >
              Thanks,
              Ron
              It's actually pretty easy to do this. You don't need to enumerate through
              all of the files in the folder either. All you need is to use the GetFiles
              method of System.IO.Direc tory.

              IE:

              string[] files = Directory.GetFi les(@"C:\images ", @"myImage.*" );

              foreach (string file in files) {
              Console.WriteLi ne("File " + file + " matches the search pattern.");
              }


              NOTE:
              There is probably another way to do this using a more recent version of the
              ..Net Framework, but our shop here has just recently purchased Visual Studio
              ..Net 2008 and have not yet installed it. We are still working with VIsual
              Studio .Net 2003 and the v1.1 framework. :P

              HTH,
              Mythran



              Comment

              • Mythran

                #8
                Re: Open file without knowing the extension



                "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                news:op.ua7b5hm z8jd0ej@petes-computer.local. ..
                On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_chapman47@ho tmail.co.uk>
                wrote:
                >
                >Enumerate all the files in the folder and see if the primary name
                >matches the name you are after, if so you have found the file!
                >
                And specifically, see the Directory.GetFi les(string, string) method, which
                allows you to pass a search string. If you have the name of the directory
                as "strDirecto ry" and the name of the property as "strPropert y", then
                calling Directory.GetFi les(strDirector y, strProperty + "*") will retrieve
                the actual filenames of every file in the directory that starts with the
                property name.
                >
                You can then enumerate those files (if any), attempting to open each one
                with Image.FromFile( ), using the first one that succeeds.
                >
                Pete
                Whoops, must have been replying when you posted this....so yeah, ignore my
                post since this one says the same :)

                Mythran


                Comment

                • Peter Duniho

                  #9
                  Re: Open file without knowing the extension

                  On Thu, 15 May 2008 08:25:28 -0700, Ron <rs_herhuth@yah oo.comwrote:
                  The only problem with that is that the path.GetExtensi on method expects
                  an extension to be specified (which seems a little counter intuitive if
                  you ask me), else it returns ""
                  The Path class (not "path") is used to manipulate strings that represent
                  paths. Whether those strings represent an actual file or not is
                  irrelevant to the Path class. It only cares about the string itself. In
                  that context, it makes perfect sense that a string without any extension
                  would return "" if there's no extension specified in the string.

                  Pete

                  Comment

                  Working...