Thursday, February 24, 2011

Class File - Holds the executable file's directory

I usually put subfolders in the directory of my executable file. Usually the following:
  • Root\Reports - for the crystal reports and other reports that my system needs
  • Root\Logs - for the audit trail of my system (if i dont include them in my database)
  • More
There always comes a time that I would have to get the directory of my executable file and add the folder name and the filename to locate/open the file. It would involve more processes if i would have to get the path of my executable file everytime a access an external file. Thus what I did were the following (given that the user has already accessed the system - means user is validated upon log-in):

Create a class that would hold the path of the executable file

Public Class clsExecfile
       Public ApplicationPath as string = ""
       Public ReportPath as string = ""
       Public LogPath as string = ""
       ' you may add more if there are still others


       Public Sub New()
                  objFile As New System.IO.FileInfo(Application.ExecutablePath)
                  ApplicationPath = objFile.Directoryname
                  ApplicationPath = iif(strings.right(ApplicationPath,1) = "\", ApplicationPath, ApplicationPath & "\")
                   ReportPath = ApplicationPath & "Reports\"
               LogPath = ApplicationPath & "Logs\"
               objFile = nothing
       End Sub

End Class

Declare the object

                Public objApplication as new clsExecFile()

Use the object
  • to assign the fullpath of a report that you want to access
dim strReport as string = objApplication.ReportPath & "rptMyReport.rpt"
  • to get the directory of the executable file
                    objApplication.ApplicationPath


You will see here that if you dont create a class that retrieves the directory of your eecutable file, you will have to write the above code every time you want to retrieve your exec files directory. Instead, by just creating a class, you have minimzed your other codes into 1 or 2 lines.

Hope this helps
              

No comments:

Post a Comment