How to specify virtual paths in C#?

J

John Holmes

I'm trying to load an xslt file from my asp.net application directory and
I'm not sure what path to use. I thought no path would give me that path,
but it appears to give me the c:\windows\system32 path. Here's my code:

XslTransform xslTran = new XslTransform();
xslTran.Load("file.xslt");

I get an error saying it can't find the file:
"C:\Windows\System32\file.xslt"

I also have this problem when trying to write and xml file using the
dataset.WriteXml method. Not sure how to use virtual paths in C# code,
please help.

John
 
N

Nikhil Tayal

You can use Server.MapPath(FileName) if your file is in the same folder as
the application.

Nikhil
 
N

Nikhil Tayal

Try this out
FileStream myFileStream = new FileStream(fileName,FileMode.Open,
FileAccess.Read);
StreamReader myXmlStream = new StreamReader(myFileStream);
xmlDoc.Load(myXmlStream);
myFileStream.Close();

Nikhil
 
S

Steven Cheng[MSFT]

Thanks for Nikhil's informative suggestions.

Hi John,

I think the "Server.MapPath" method Nikhil has mentioned is the one you
need. Here is the function's reference in MSDN:

#HttpServerUtility.MapPath Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebhttpserveru
tilityclassmappathtopic.asp?frame=true

#Server.MapPath
http://msdn.microsoft.com/library/en-us/iissdk/iis/ref_vbom_serommp.asp?fram
e=true


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top