Streamreader Directory

I'm working with StreamReader in my Asp.Net mvc application.

I'm having an issue getting the StreamReader to use the root of my application, and not the C:// drive on my machine.

I have the following:

 public ActionResult Test()
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Test));
                TextReader textReader;
                textReader = new StreamReader("../Content/items.xml");
                Test test = (Test)serializer.Deserialize(textReader);
                textReader.Close();
                return View(test);
            }
Jon Skeet
people
quotationmark

When you run a web application, the current working directory of the process isn't the directory containing your source code. You might want to look at HttpServerUtility.MapPath or HostingEnvironment.MapPath.

Note that this doesn't really have anything to do with StreamReader - for diagnostic purposes, you'd be better off with something like:

FileInfo file = new FileInfo("../Content/items.xml");
Debug.WriteLine(file.FullName);

people

See more on this question at Stackoverflow