locked
File not found exception: I can see the file is there! RRS feed

  • Question

  • User-1306380974 posted

    Windows 8. Visual Studio for web 2013. Trying to make a web page that stores a small amount of data in a text file. StreamReader can never find the file. Exception says:

    Could not find file 'C:\Users\Tom\Documents\Visual Studio 2013\WebSites\WebSite2\FeatureList.txt'.

    Sounds to me like the file isn't there, but if I paste that path into windows explorer, notepad launches and shows me the contents of the file. I've found a number of ways not to find this file:

            StreamReader sr = File.OpenText(Server.MapPath("~") + @"data\FeatureList.txt");
            //StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~"), @"data/FeatureList.txt"));
            //string path = (string.Format("{0}", AppDomain.CurrentDomain.BaseDirectory,@"\data\FeatureList.txt"));
            //StreamReader sr = new StreamReader(path);
            //StreamReader sr = new StreamReader(Server.MapPath("~data/FeatureList.txt"));
            String FileText = sr.ReadToEnd().ToString();
            sr.Close();
    

    Perhaps it's a permission error? If I look at the permissions for the data folder, I see 3 users, SYSTEM, me and Administrator, and not the Default App Pool or IIS users mentioned in "Deploying an ASP.NET website" article I found at asp.net.

    I wrote a similar web page a couple years ago that reads but doesn't write and it runs to this day at godaddy.

    Tuesday, August 5, 2014 5:06 PM

Answers

  • User-1360095595 posted
    StreamReader sr = File.OpenText(Server.MapPath("~/data/FeatureList.txt"));

    Assuming your file in located in the data folder inside your root website. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 5:19 PM
  • User-1716253493 posted
    StreamReader sr = File.OpenText(Path.Combine(Server.MapPath("~/data"),"FeatureList.txt"));

    Try run VS2013 as administrator

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 10:13 PM

All replies

  • User-760709272 posted

    Add "Everyone" to the list of users on the WebSite2 folder and give it full control.

    Tuesday, August 5, 2014 5:13 PM
  • User-1360095595 posted
    StreamReader sr = File.OpenText(Server.MapPath("~/data/FeatureList.txt"));

    Assuming your file in located in the data folder inside your root website. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 5:19 PM
  • User-1716253493 posted
    StreamReader sr = File.OpenText(Path.Combine(Server.MapPath("~/data"),"FeatureList.txt"));

    Try run VS2013 as administrator

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 10:13 PM