user1108282
user1108282

Reputation: 65

Path to read file from inside www folder

I have a binary reader to read a file

BinaryWriter bw2 = new BinaryWriter(File.Open(@"c:\test\test6.xml", FileMode.OpenOrCreate));

the path i have set to is c:\test\test.xml However it needs to read the file from www folder hosted site so www\test\test.xml should it be ~\test\test.xml? Not sure. Thanks for your help

Upvotes: 0

Views: 64

Answers (2)

n8wrl
n8wrl

Reputation: 19765

Check out Server.MapPath() http://msdn.microsoft.com/en-us/library/ms524632(v=VS.90).aspx

So in your case, you're after this:

using (BinaryWriter bw2 = new BinaryWriter(File.Open(Server.MapPath(@"~\test\test6.xml", FileMode.OpenOrCreate)))
{
    ...
}

Notice I added the using() which is a best-practice for working with expensive resources like files.

Of course, you really should seperate out file opening from object creation so you can have better diagnostics in your code.

Upvotes: 2

Rafael
Rafael

Reputation: 2817

Probably you need this function: http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

Upvotes: 1

Related Questions