Thursday, March 22, 2012

Trouble with File Paths

Hi,

On my development machine I am using VB/ASP .net 2.0, IIS 5.1 , Windows XP Pro. The test server has Windows Server 2003, IIS 6.0.

I am trying to access my config settings located in an .xml file I created (SQLMessageSettings.xml).
The physical location is: C:\VB.Net 2.0 Projects\SQLMessageViewer\SQLMessageViewer\SQLMessageSettings.xml

When I run the application it runs from IIS (not VS Development Server) as a virtual IIS application.

I thought that to access the "SQLMessageSettings.xml" file I can use the file path "/SQLMessageViewer/SQLMessageSettings.xml" and it would start at the root of my website and find the file.
Instead I get an error "Could not find a part of the path 'C:\SQLMessageViewer\SQLMessageSettings.xml'."
If I remove the initial "/" the error is "Could not find a part of the path 'C:\WINDOWS\system32\SQLMessageViewer\SQLMessageSettings.xml'."
The only way it works is if I put the entire file path in (C:\VB.Net 2.0 Projects\SQLMessageViewer\SQLMessageViewer\SQLMessageSettings.xml).

When I changed the app to run from the VS Development Server it worked with only "SQLMessageViewer/SQLMessageSettings.xml"

I know I am missing something here. Part of the problem is that I don't fully understand all the architechture of IIS as to how a Website, Virtual Application and Virtual Directory all relate to each other.

How can set it up so I don't have to put the entire physical file path?
Would the tilde, "~", help me in anyway?
Also is there a way to do it so that I won't have to change the file path reference when I move it to the test server even though the physical location of the files are different?

Thank You,

Oran

As far as I know, you always have to use the full path to access a file on the server using the filesystem object. But, you can MAP the path from your current app. I think this should work

Dim FilePath as string = Server.MapPath("./SQLMessageViewer/SQLMessageSettings.xml")


Hi,

I think ~ should help you to solve the problem. ~ points to the root folder of your web application and then you can specify the path as you require.

Hope this helps to solve the problem

Good LuckYes

Regards

Vineed


Thanks for the suggestion but when I tried the "~",

"~/SQLMessageViewer/SQLMessageSettings.xml"

I got this:

Could not find a part of the path 'C:\VB.Net 2.0 Projects\SQLMessageViewer\~\SQLMessageViewer\SQLMessageSettings.xml'.

That is the right file path except for the "~."

I decided to get rid of the extra "SQLMessageViewer" folder and put everything where the .sln file is and it didn't help.

I tried this:

"~/SQLMessageSettings.xml"

and got this:

Could not find a part of the path 'C:\WINDOWS\system32\~\SQLMessageSettings.xml'.

Thanks,

Oran


Hi Oran,

Try usingRequest.ApplicationPath. This property returns the Virtual path of the Web applications Root folder. To this path you can add the path of your file as required. If you are not comfortable using the Virtual Path then you can also try using theRequest.PhysicalApplicationPath. This would return the Physical Path of the Web Application's root folder. You can again add the path of the file as required.

Hope this helps to solve your problem

Good LuckYes

Regards

Vineed


Try this

Server.MapPath("~/SQLMessageViewer/SQLMessageSettings.xml")

Assumption is that SQLMessageViewer is a folder inside your website


Hi,

My team has built a configuration component based on the provider model allowing the developer to store the config settings wherever he wants (database, .config file, .xml file ...). The component is a class library compiled to a dll (CommonConfig.dll). The dll is meant to serve both windows apps and web apps. The code in the commonconfig.dll that loads the xml file looks like this:

xd =New XmlDocument()
xd.Load(MyBase.Location)

The location (file path, database connection) of the config settings is stored in the web.config (or app.config) file like this:

<ConfigSettingslocation="~/SQLMessageSettings.xml"type="Common.Config.CommonConfig.XmlCommonConfigProvider"/>

Does "server.mappath" work in a windows app? Is it only a matter of importing the right namespace (which one?)? We can make 2 versions of the CommonConfig.dll one for web and one for windows, but we would like to avoid that if possible.

I was hoping that there was a way to locate the file with only the file path (and without having to write out the entire path). It could be there is no better solution, but it is strange that when I changed the app to run from the VS Development Server it worked fine with "SQLMessageViewer/SQLMessageSettings.xml" (See original post).

Thanks,

Oran


System.Web.HttpContext.Current.Server.MapPath is the fully qualified name . Well in your config dll, you can check if the httpContext.Current is empty .. if so then its not a web app so with this check your config dll can decide how to pull the config file


Thanks Jeev! Your solution works for both windows and web (on both servers).

I don't understand why the tilde doesn't work on its own without server.mappath?

Oran

0 comments:

Post a Comment