Results 1 to 6 of 6

Thread: Check if a file exists in a directory

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Check if a file exists in a directory

    Ok i want to check if a file exists in the Application.StartupPath. I tried My.Computer.FileSystem.FileExists but that checks the whole computer doesn't it. ? How do i check if a file exists in a directory?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check if a file exists in a directory

    My.Computer.FileSystem.FileExists but that checks the whole computer doesn't it
    Not to the best of my knowledge, and MSDN implies that you need to specify the full path. As far as I can tell FielExists is exactly what you want - you just pass in the application startuppath as part of the argument.
    Last edited by keystone_paul; Sep 5th, 2009 at 02:21 PM.

  3. #3

  4. #4

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check if a file exists in a directory

    Hmm maybe I'm looking at a different namespace. There seem to be several FileExists methods scattered around the .Net framework!

    This works fine for me :

    Code:
    Imports Microsoft.VisualBasic.FileIO
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim x As Boolean = FileSystem.FileExists("WisCrPcs.exe")
            Dim y As Boolean = FileSystem.FileExists("C:\Elements\patch\Clean\WisCrPcs.exe")
    
        End Sub
    
    End Class
    I just picked a random file from my filesystem and tried two calls - one with full path, one without.

    The full path version works (y=true), the filename only doesn't (x=false)

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Check if a file exists in a directory

    For a specific file in a specific directroy, I use the System.IO.DirectoryInfo ... class ( the constructor takes a directory path), then use the FileExists function of the DI instance to find the file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width