filename

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

    filename

    Hi,

    How can check using .NET 1.1 that a string contains a valid filename
    for winxp?

    The application in question has a textbox where user can enter
    filename and only the filename. It should not allowed to enter path
    +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
    \myfile.log" - no directory info should be allowed, only the filename.

    Any suggestions.
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: filename

    if(File.Exists( filePathFromTex tBox))
    {
    //Do work
    }
    else
    {
    //Display error
    }

    Exists has been around since the early days. I believe it was 1.0. It was
    definitely 1.1:
    Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.


    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "RedLars" <Liverpool1892@ gmail.comwrote in message
    news:9e54d9f3-9857-4e17-bcdc-cad21abe2ba9@x3 5g2000hsb.googl egroups.com...
    Hi,
    >
    How can check using .NET 1.1 that a string contains a valid filename
    for winxp?
    >
    The application in question has a textbox where user can enter
    filename and only the filename. It should not allowed to enter path
    +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
    \myfile.log" - no directory info should be allowed, only the filename.
    >
    Any suggestions.

    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: filename

      You should just need to check if the string contains any invalid filename
      characters as returned by Path.GetInvalid FilenameChars.





      "RedLars" wrote:
      Hi,
      >
      How can check using .NET 1.1 that a string contains a valid filename
      for winxp?
      >
      The application in question has a textbox where user can enter
      filename and only the filename. It should not allowed to enter path
      +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
      \myfile.log" - no directory info should be allowed, only the filename.
      >
      Any suggestions.
      >

      Comment

      • rhaazy

        #4
        Re: filename

        On Sep 24, 9:45 am, RedLars <Liverpool1...@ gmail.comwrote:
        Hi,
        >
        How can check using .NET 1.1 that a string contains a valid filename
        for winxp?
        >
        The application in question has a textbox where user can enter
        filename and only the filename. It should not allowed to enter path
        +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
        \myfile.log" - no directory info should be allowed, only the filename.
        >
        Any suggestions.
        Just check your string to make sure there are no occurences of \
        ..IndexOf("\");

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: filename

          On Sep 24, 9:45 am, RedLars <Liverpool1...@ gmail.comwrote:
          Hi,
          >
          How can check using .NET 1.1 that a string contains a valid filename
          for winxp?
          >
          The application in question has a textbox where user can enter
          filename and only the filename. It should not allowed to enter path
          +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
          \myfile.log" - no directory info should be allowed, only the filename.
          >
          Any suggestions.
          I do not of any method in File class that check if a name is valid.
          the simplest thing is to check for the \ character. You could use a
          regular expression to better check for invalid chars.

          Comment

          • OD

            #6
            Re: filename

            the best is certainly using a regular expression.
            following sample check long file name validity :
            ^[^\\\./:\*\?\"<>\|]{1}[^\\/:\*\?\"<>\|]{0,254}$

            --


            OD___



            Comment

            • RedLars

              #7
              Re: filename

              Let me re-phrase the question.

              By opening notepad, selecting Save as.. and typing in 'g/fgh.txt' the
              application wont save the file because the filename is invalid.

              How can I using .NET 1.1 check if a given string contains only a valid
              filename? It should not include path and filename.

              Thank you.

              >On 24 Sep, 15:51, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo... @comcast.netNoS pamMwrote:
              if(File.Exists( filePathFromTex tBox))
              {
                 //Do work}
              >
              else
              {
                  //Display error
              >
              }
              >
              Exists has been around since the early days. I believe it was 1.0. It was
              definitely 1.1:http://msdn.microsoft.com/en-us/libr...members(VS.71)...
              >
              --
              Gregory A. Beamer
              MVP, MCP: +I, SE, SD, DBA
              >
              Subscribe to my bloghttp://feeds.feedburne r.com/GregoryBeamer#
              >
              or just read it:http://feeds.feedburner.com/GregoryBeamer
              >
              *************** *************** **************
              | Think outside the box!                               |
              *************** *************** **************" RedLars" <Liverpool1...@ gmail.comwrote in message
              >
              news:9e54d9f3-9857-4e17-bcdc-cad21abe2ba9@x3 5g2000hsb.googl egroups.com...
              >
              Hi,
              >
              How can check using .NET 1.1 that a string contains a valid filename
              for winxp?
              >
              The application in question has a textbox where user can enter
              filename and only the filename. It should not allowed to enter path
              +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
              \myfile.log" - no directory info should be allowed, only the filename.
              >
              Any suggestions.

              Comment

              • Jay Riggs

                #8
                Re: filename

                On Sep 24, 6:45 am, RedLars <Liverpool1...@ gmail.comwrote:
                Hi,
                >
                How can check using .NET 1.1 that a string contains a valid filename
                for winxp?
                >
                The application in question has a textbox where user can enter
                filename and only the filename. It should not allowed to enter path
                +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                \myfile.log" - no directory info should be allowed, only the filename.
                >
                Any suggestions.
                RedLars,

                If you want to check if a given string could be a valid filename only
                (even for files that don't exist), you can use a RegEx.

                I've been using this one lately:
                ^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$

                Which I got here:


                A word of warning: using just this regex will not allow you to
                identify the several strings Windows will not allow for filenames
                (such as nul, com1, etc.). I've been regexs that covered includes
                there reserved words can can't find it now.


                HTH
                -Jay



                Comment

                • Jay Riggs

                  #9
                  Re: filename

                  On Sep 24, 7:48 am, Family Tree Mike
                  <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                  You should just need to check if the string contains any invalid filename
                  characters as returned by Path.GetInvalid FilenameChars.
                  >
                  http://msdn.microsoft.com/en-us/libr...getinvalidfile...
                  >
                  >
                  >
                  "RedLars" wrote:
                  Hi,
                  >
                  How can check using .NET 1.1 that a string contains a valid filename
                  for winxp?
                  >
                  The application in question has a textbox where user can enter
                  filename and only the filename. It should not allowed to enter path
                  +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                  \myfile.log" - no directory info should be allowed, only the filename.
                  >
                  Any suggestions.- Hide quoted text -
                  >
                  - Show quoted text -

                  Unfortunately, you can't use this method in 1.1.

                  Comment

                  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

                    #10
                    Re: filename

                    Oops! You're right, I missed that in the doc. Sorry!

                    "Jay Riggs" wrote:
                    On Sep 24, 7:48 am, Family Tree Mike
                    <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                    You should just need to check if the string contains any invalid filename
                    characters as returned by Path.GetInvalid FilenameChars.

                    http://msdn.microsoft.com/en-us/libr...getinvalidfile...



                    "RedLars" wrote:
                    Hi,
                    How can check using .NET 1.1 that a string contains a valid filename
                    for winxp?
                    The application in question has a textbox where user can enter
                    filename and only the filename. It should not allowed to enter path
                    +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                    \myfile.log" - no directory info should be allowed, only the filename.
                    Any suggestions.- Hide quoted text -
                    - Show quoted text -
                    >
                    >
                    Unfortunately, you can't use this method in 1.1.
                    >

                    Comment

                    • Ignacio Machin ( .NET/ C# MVP )

                      #11
                      Re: filename

                      Exists has been around since the early days. I believe it was 1.0. It was
                      definitely 1.1:http://msdn.microsoft.com/en-us/libr...members(VS.71)...
                      >
                      I think that he wants to know if a given string is a valid file name,
                      not to check if a file exist or not

                      Comment

                      • G.S.

                        #12
                        Re: filename

                        On Sep 24, 1:07 pm, Family Tree Mike
                        <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                        Oops!  You're right, I missed that in the doc.  Sorry!
                        >
                        >
                        >
                        "Jay Riggs" wrote:
                        On Sep 24, 7:48 am, Family Tree Mike
                        <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                        You should just need to check if the string contains any invalid filename
                        characters as returned by Path.GetInvalid FilenameChars.
                        >>
                        "RedLars" wrote:
                        Hi,
                        >
                        How can check using .NET 1.1 that a string contains a valid filename
                        for winxp?
                        >
                        The application in question has a textbox where user can enter
                        filename and only the filename. It should not allowed to enter path
                        +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                        \myfile.log" - no directory info should be allowed, only the filename.
                        >
                        Any suggestions.- Hide quoted text -
                        >
                        - Show quoted text -
                        >
                        Unfortunately, you can't use this method in 1.1.- Hide quoted text -
                        >
                        - Show quoted text -
                        After checking for invalid characters following the above suggestions,
                        would it be feasible to try to create a temp file just to see if your
                        host/target OS will complain?

                        Comment

                        • Ignacio Machin ( .NET/ C# MVP )

                          #13
                          Re: filename

                          On Sep 24, 4:27 pm, "G.S." <gstoy...@gmail .comwrote:
                          On Sep 24, 1:07 pm, Family Tree Mike
                          >
                          >
                          >
                          <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                          Oops! You're right, I missed that in the doc. Sorry!
                          >
                          "Jay Riggs" wrote:
                          On Sep 24, 7:48 am, Family Tree Mike
                          <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                          You should just need to check if the string contains any invalid filename
                          characters as returned by Path.GetInvalid FilenameChars.
                          >>
                          "RedLars" wrote:
                          Hi,
                          >
                          How can check using .NET 1.1 that a string contains a valid filename
                          for winxp?
                          >
                          The application in question has a textbox where user can enter
                          filename and only the filename. It should not allowed to enter path
                          +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                          \myfile.log" - no directory info should be allowed, only the filename.
                          >
                          Any suggestions.- Hide quoted text -
                          >
                          - Show quoted text -
                          >
                          Unfortunately, you can't use this method in 1.1.- Hide quoted text -
                          >
                          - Show quoted text -
                          >
                          After checking for invalid characters following the above suggestions,
                          would it be feasible to try to create a temp file just to see if your
                          host/target OS will complain?
                          Yes, But that would throw an exception, which is not a very good
                          approach, you can do something like:

                          bool isvalid(string str){
                          try{
                          string path = Path.GetTempFil eName() + str;
                          File.Create( path).Close();
                          File.Delete( path);
                          return true;
                          }catch{
                          return false;
                          }
                          }

                          Comment

                          • Jay Riggs

                            #14
                            Re: filename

                            On Sep 24, 1:27 pm, "G.S." <gstoy...@gmail .comwrote:
                            On Sep 24, 1:07 pm, Family Tree Mike
                            >
                            >
                            >
                            >
                            >
                            <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                            Oops!  You're right, I missed that in the doc.  Sorry!
                            >
                            "Jay Riggs" wrote:
                            On Sep 24, 7:48 am, Family Tree Mike
                            <FamilyTreeM... @discussions.mi crosoft.comwrot e:
                            You should just need to check if the string contains any invalid filename
                            characters as returned by Path.GetInvalid FilenameChars.
                            >>
                            "RedLars" wrote:
                            Hi,
                            >
                            How can check using .NET 1.1 that a string contains a valid filename
                            for winxp?
                            >
                            The application in question has a textbox where user can enter
                            filename and only the filename. It should not allowed to enter path
                            +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                            \myfile.log" - no directory info should be allowed, only the filename.
                            >
                            Any suggestions.- Hide quoted text -
                            >
                            - Show quoted text -
                            >
                            Unfortunately, you can't use this method in 1.1.- Hide quoted text -
                            >
                            - Show quoted text -
                            >
                            After checking for invalid characters following the above suggestions,
                            would it be feasible to try to create a temp file just to see if your
                            host/target OS will complain?- Hide quoted text -
                            >
                            - Show quoted text -
                            G.S.

                            After checking for invalid characters in the filename why not just
                            attempt to save the file where you want it to go and handle any
                            exceptions as they occur? This seems the more direct approach unless
                            I'm missing something. Saving to a temp location successfully only
                            tells you you can save it to the temp location.

                            -Jay

                            Comment

                            • Jay Riggs

                              #15
                              Re: filename

                              On Sep 24, 9:43 am, Jay Riggs <jay.s.ri...@gm ail.comwrote:
                              On Sep 24, 6:45 am, RedLars <Liverpool1...@ gmail.comwrote:
                              >
                              Hi,
                              >
                              How can check using .NET 1.1 that a string contains a valid filename
                              for winxp?
                              >
                              The application in question has a textbox where user can enter
                              filename and only the filename. It should not allowed to enter path
                              +filename like "c:\tmp\myfile. log" or relative paths "..\tmp
                              \myfile.log" - no directory info should be allowed, only the filename.
                              >
                              Any suggestions.
                              >
                              RedLars,
                              >
                              If you want to check if a given string could be a valid filename only
                              (even for files that don't exist), you can use a RegEx.
                              >
                              I've been using this one lately:
                              ^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$
                              >
                              Which I got here:http://regexlib.com/REDetails.aspx?regexp_id=2286
                              >
                              A word of warning: using just this regex will not allow you to
                              identify the several strings Windows will not allow for filenames
                              (such as nul, com1, etc.).  I've been regexs that covered includes
                              there reserved words can can't find it now.
                              >
                              HTH
                              -Jay
                              Boy, I really mangled the last part of my response above. What I
                              meant to say was that the regex I provided a link to in my previous
                              message will check a potential filename for characters that Windows
                              will not allow, but will not check for filenames that Windows
                              specifically disallows. Examples include nul, com1, etc.

                              I found the regex that handles this at the following:


                              I ended up using the modified version found on the first user comment
                              for the regex. It worked fine for me.

                              -Jay

                              Comment

                              Working...