Monday, September 24, 2007

How to retrieve absolute physical/ virtual path inside vb class

recently my friends ask me how to write a code to retrieve absolute path from vb class for his asp.net application. the problem is server.mappath function cannot be used inside vb class so he ask for alternative.

so here's the solution that i think is good for sharing with everyone:

' import declarative
imports system.web

private strPath as string = Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Dashboard.mdb")
Private sqlconn As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0; data source=" & strPath & ";Jet OLEDB:Database Password=Dashboard; ")

the function HostingEnvironment.MapPath used to retrieve the physical / virtual path in the server. for more info about the HostingEnvironment classes you can go here:
Hosting Environment Class

that's it, hope it useful for everyone..

No comments: