473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

MapPath equivalent from static method

Hi,
I have an xml config file for my website, which stores all the
information required to connect to a database. I have used the following
code to get the xml file into my program as an object, and this works
beautifully, as long as I directly call it from my codebehind.

Dim MyConfigXml As New System.Xml.XmlDataDocument
MyConfigXml.Load(aHttpServUtil.MapPath("My.config" ))

However, as the settings are common I would like to use this in a static
method (sorry, just realised, I mean shared function - I learnt programing
in java mainly, so I sometimes mix up the terms) of an object, and call it
from all my pages. Unfortunately, when I do
this the MapPath gives compiler errors, and I have to manually enter the
entire path into the method. Are there any other ways of getting the path -
the path of the config file is the same as that of all of my .vb files. I am
using the VB.net language, .net framework 1.1 and VS.net 2003.

Thanks,
Martin

Apr 4 '06 #1
4 2994
You forgot to mention what is the exact compilation error you have. A common
error in this context could be to try to access a non shared member inside a
shared member.

You may want also to have a look at the documentation for the "web.config"
file. This is the usual configuration file for web applications and it could
provide perhaps what you are trying to do...

--
Patrice

"Martin Eyles" <ma**********@NOSPAMbytronic.com> a écrit dans le message de
news: 12*************@corp.supernews.com...
Hi,
I have an xml config file for my website, which stores all the
information required to connect to a database. I have used the following
code to get the xml file into my program as an object, and this works
beautifully, as long as I directly call it from my codebehind.

Dim MyConfigXml As New System.Xml.XmlDataDocument
MyConfigXml.Load(aHttpServUtil.MapPath("My.config" ))

However, as the settings are common I would like to use this in a static
method (sorry, just realised, I mean shared function - I learnt programing
in java mainly, so I sometimes mix up the terms) of an object, and call it
from all my pages. Unfortunately, when I do
this the MapPath gives compiler errors, and I have to manually enter the
entire path into the method. Are there any other ways of getting the
path -
the path of the config file is the same as that of all of my .vb files. I
am
using the VB.net language, .net framework 1.1 and VS.net 2003.

Thanks,
Martin

Apr 4 '06 #2
"Patrice" <sc****@chez.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
You forgot to mention what is the exact compilation error you have. A
common error in this context could be to try to access a non shared member
inside a shared member.
Ok, here is what you need to know I think. If I used the class directly, I
get "Reference to a non-shared member requires an object reference.". If I
try to make an object of this class, I get an error when trying to
instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".

You may want also to have a look at the documentation for the "web.config"
file. This is the usual configuration file for web applications and it
could provide perhaps what you are trying to do...
I want my own config file, with just my own configuration data - this makes
it easy to install on different server setups with different database
servers. It also makes it harder for us to break the system by changing the
wrong thing in web.config. Thanks for the idea though - it is appreciated

Thanks,
Martin
"Martin Eyles" <ma**********@NOSPAMbytronic.com> a écrit dans le message
de news: 12*************@corp.supernews.com...
Hi,
I have an xml config file for my website, which stores all the
information required to connect to a database. I have used the following
code to get the xml file into my program as an object, and this works
beautifully, as long as I directly call it from my codebehind.

Dim MyConfigXml As New System.Xml.XmlDataDocument
MyConfigXml.Load(aHttpServUtil.MapPath("My.config" ))

However, as the settings are common I would like to use this in a static
method (sorry, just realised, I mean shared function - I learnt
programing in java mainly, so I sometimes mix up the terms) of an object,
and call it from all my pages. Unfortunately, when I do
this the MapPath gives compiler errors, and I have to manually enter the
entire path into the method. Are there any other ways of getting the
path -
the path of the config file is the same as that of all of my .vb files. I
am
using the VB.net language, .net framework 1.1 and VS.net 2003.

Apr 4 '06 #3
Hi Martin!
If I try to make an object of this class, I get an error
when trying to instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".


You just have to use the current HttpContext:

string s = HttpContext.Current.Server.MapPath("file.xml");
This only works, if there is a current HttpContext. So you cannot use this
e.g. in static constructors, or in background Threads you have created
(HttpContext.Current is null under this circumstances). But if you use this
in a static functions, witch is called somewhere in the middle of a Http -
Request, it is no problem.

OK?
br, GP

Apr 4 '06 #4
"Günter Prossliner" <g.prossliner/gmx/at> wrote in message
news:ex****************@TK2MSFTNGP12.phx.gbl...
Hi Martin!
If I try to make an object of this class, I get an error
when trying to instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".


You just have to use the current HttpContext:

string s = HttpContext.Current.Server.MapPath("file.xml");
This only works, if there is a current HttpContext. So you cannot use this
e.g. in static constructors, or in background Threads you have created
(HttpContext.Current is null under this circumstances). But if you use
this in a static functions, witch is called somewhere in the middle of a
Http - Request, it is no problem.

OK?
br, GP


Thanks very much - it works a treat. :-)

Martin
Apr 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Groh | last post by:
Hi, I'm trying to use the Server.MapPath in my ClassLibrary application, but I didn'get it...I tryed already the System.Web.HttpServerUtility... OpenConn(); DataSet dsErrorSQL = new DataSet();...
6
by: Daniel Groh | last post by:
Hi Joe, no it's not static...yes..it's static when u have a webapplication and i have a class library application, try it and tell me, may be could be a problem in my framework lib u know ? but i...
3
by: MattB | last post by:
If I use server.mappath to get to the root dir of my application from within codebehind, it works fine. I also want to use this from a vb class I have in my application, and I think I have an issue...
6
by: Nathan Sokalski | last post by:
When using the Server.MapPath() method, the results being returned are given as locations on my hard drive. I would like to be returned a result that is a URL, in my case something like the...
12
by: rodchar | last post by:
hey all, vb has a static keyword for variables, what is charp's equivalent,please? static tempVar as String thanks, rodchar
21
by: clintonG | last post by:
Will somebody convert this for me... System.Web.UI.HtmlControls.HtmlHead header; header = TryCast(this.Page.Header, System.Web.UI.HtmlControls.HtmlHead); <%= Clinton Gallagher NET csgallagher...
7
by: teo | last post by:
I need to use the 'Server.MapPath' function in the 'Session_End' event of the Global.asax file (to reach a folder and the clean some temporary files up), but it doesn't work: Sub...
4
by: vunet.us | last post by:
How to use server.mappath() parent folder correctly: Server.MapPath("../test.asp") Thank you for the hint.
3
by: rn5a | last post by:
Server.MapPath returns the physical file path that corresponds to the specified virtual path whereas Request.MapPath maps the specified virtual path to a physical path. Assuming that a file named...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.