Problem Checking If A File Exists In VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkslide
    New Member
    • Jun 2007
    • 4

    Problem Checking If A File Exists In VB.NET

    Hi,
    I'm checking if a file exists with the following code;

    Code:
    If System.IO.File.Exists("C:\test.txt") = True Then
         MsgBox("File Exists")
    Else
          MsgBox("File Does Not Exist")
    End If
    For some reason it always returns false even though the file does exist. Any suggestions? Could this be a security issue?
    Last edited by Frinavale; Dec 15 '09, 05:35 PM. Reason: Please post code in [code] ... [code] tags. Added code tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The reason it always says false is that the file doesn't exist, know why? Because you have a \t in there, which means "put a tab character here".
    It's an easy miss.
    Do this:
    [code=vb]
    If System.IO.File. Exists("C:\\tes t.txt") = True Then
    MsgBox("File Exists")
    Else
    MsgBox("File Does Not Exist")
    End If
    [/code]

    Comment

    • darkslide
      New Member
      • Jun 2007
      • 4

      #3
      Originally posted by Plater
      The reason it always says false is that the file doesn't exist, know why? Because you have a \t in there, which means "put a tab character here".
      It's an easy miss.
      Do this:
      [code=vb]
      If System.IO.File. Exists("C:\\tes t.txt") = True Then
      MsgBox("File Exists")
      Else
      MsgBox("File Does Not Exist")
      End If
      [/code]
      Thanks for the reply,

      but that unfortunately didn't resolve it. I don't think it's the code I think it's an Environment issue. I went back and tried the same code in VS.NET 2003 and it works. But it won't work in VS.NET 2005. Any other suggestions would be greatly appreciated.

      Thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I think it is *your* specific environment. I use vs2005 and copy pasted that code and it worked correctly.
        Told me file didn't exist, then I create the file and it told me it did exist.

        Are you SURE the file actually exists?

        Comment

        • darkslide
          New Member
          • Jun 2007
          • 4

          #5
          lol...yeah the file exists. That was the first thing I checked for.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            not sure what to tell ya then, works fine for me in vs2005.

            Comment

            • darkslide
              New Member
              • Jun 2007
              • 4

              #7
              Thanks anyways for your help. Now I'm running vs2003 and vs2005 on the same computer. Do you think that could be causing my issue?

              Comment

              • sjcrichton
                New Member
                • Jun 2007
                • 1

                #8
                Hey Mr D,

                I've had problems with this all afternoon. It ended up being the naming of a file in XP. My code was looking for file.jpg but the file was named file.jpg.jpg. Double check you haven't got an extra extension.

                Comment

                • dip_developer
                  Recognized Expert Contributor
                  • Aug 2006
                  • 648

                  #9
                  Originally posted by darkslide
                  Hi,
                  I'm checking if a file exists with the following code;

                  If System.IO.File. Exists("C:\test .txt") = True Then
                  MsgBox("File Exists")
                  Else
                  MsgBox("File Does Not Exist")
                  End If

                  For some reason it always returns false even though the file does exist. Any suggestions? Could this be a security issue?
                  try this.........

                  [CODE=vbnet]
                  Dim filename as String="C:\test .txt"
                  Dim fFile As New FileInfo(filena me)
                  If Not fFile.Exists Then
                  MsgBox("File Doesn't Exist")
                  Else
                  MsgBox("File Exists")
                  End If
                  [/CODE]

                  Comment

                  • TasChew
                    New Member
                    • Dec 2009
                    • 1

                    #10
                    My suggestion may come too late but anyway...

                    If you create a project and assign the project location to a network, you will probably get this message:

                    "The project location is not trusted:
                    Running the application may result in security exceptions when it attempts to perform actions which require full trust.
                    "

                    Check the project location where you save your project. Copy the project folder to a local c drive and it should work. Tested working using VS.NET 2005.

                    In short, the File.Exists code does not work if your project is saved to a network.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      I was just about to say, it's probably because your application is running under a Windows user account with permissions that don't let you access the C:\ directory.

                      -Frinny

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        If you don't have permision, a security exception should have been thrown

                        Comment

                        • toirleach
                          New Member
                          • Aug 2010
                          • 1

                          #13
                          same issue

                          All,

                          I just had the same problem here on VS2005. It worked fine on my own PC and even worked from my PC when run from a network drive. I then tested it from another PC and it wouldn't work from the network drive. No exceptions thrown or anything. File.Exists just returned false for a file that existed. Directory.Exist s did the same for a directory that existed.

                          I then copied the exe onto the required PC and it worked fine!!

                          Bit of a pain!! An hour of my life gone!! :)

                          T.

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            .NET apps running from a network drive run under a different set of "rules" (they have the "zones" management style like that of internet explorer)
                            There are some hoops you can jump through to get it to owkr (configure per computer, use clickOnce deployment) but it is probably easier to just run the app locally.

                            Comment

                            Working...