Hi there again,
You must have misunderstood my previous answer, as I posted the solution to your problem there already, albeit a bit short.
It's not really that the usage of the MapPath method differs depending on where it is used, it's the namespace that hosts the method that is important. The method must be reachable from within the current code.
The MapPath method lives in the
HttpServerUtility class, which in turn lives in the System.Web namespace. This means that outside this namespace, or without referencing this namespace, you cannot call this method.
If you think about it a bit, it makes sense. What would MapPath do do for a file in a Windows app? Since you are already working with physical locations, the answer is: probably nothing.
Now, a component is a bit different. A component can be used in multiple other projects: Web applications, Web services, Desktop apps, console apps etc. Your component is "accidentally" used in a Web app. This means that by default it's not "web aware". To become web aware, you need to get a reference to the "current"
httpcontext: that is, the context of the application that uses your component (still with me??)
So, if you needed to use other Web stuff, like Response.Write, Request.ServerVariables, here's what you could do:
Code:
' Get the current context
Dim currentContext As System.Web.HttpContext = System.Web.HttpContext.Current
' With this context object, you can do what
' you usually do. A few examples:
Dim test As String = currentContext.Request.QueryString.Get("Test")
' Or use MapPath
Dim myString As String = currentContext.Server.MapPath("Bla.txt")
You are probably not aware of the complexity behind all this, because most of it is shielded from you in day to day operations. For your convenience, the developers of ASP.NET have added Server, Request, Response, etc properties to the Page class for you. Since the Page class (or any derived child) is the "current" class (Me, or this), there is no need to explicitly reference that class. So, Me.Server.MapPath is the same as Server.MapPath when used within a page.
Does this clarify things?
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.