spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
 
Home arrow Source Codes arrow Visual Basic arrow How to Check if a File Exists

How to Check if a File Exists PDF Print E-mail
Source Codes - Visual Basic
Written by Thomas Kaloyani   
VB Source CodesAn efficient way to check if a file exists or not, regardless of the file attributes, is using the OpenFile API.

The following code shows how to make the API call.

Usage:

if FileExists("C:\MyFile.txt") then
   Debug.Print  "Exists"
else
    Debug.Print "Don't Exists"
end if



Declarations:
Private Const OF_EXIST         As Long = &H4000
Private Const OF_ERROR          As Long = -1
Private Const OFS_MAXPATHNAME  As Long = 128

Private Type OFSTRUCT
    cBytes As Byte
    fFixedDisk As Byte
    nErrCode As Integer
    Reserved1 As Integer
    Reserved2 As Integer
    szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long

Code:
Private Function FileExists(ByVal sFile As String) As Boolean
    Dim lRetVal As Long
    Dim of As OFSTRUCT
   
    lRetVal = OpenFile(sFile, of, OF_EXIST)
    If lRetVal <> OF_ERROR Then
        FileExists = True
    Else
        FileExists = False
    End If
End Function


Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website www.Kaloyani.com or through VBprofs.com - the newest Visual Basic and VB.NET resources portal.

Source:
www.VBprofs.com

< Prev   Next >

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer