0

I'm trying to save a file to the server and then load into a reader for it to be downloaded. However, I am getting a FileNotFoundExeption. I save to the exact same path, manually open the directory and can see the file there. However, reading it results in the exception. This is my first time trying his - am I doing something wrong?

try 
{
    using (StreamReader reader = new 
           StreamReader(HttpContext.Current.Server.MapPath(@"~/Downloads/data.text"))) 
    {
       // do something
    }
}
catch (Exception) 
{

}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • Did you check the permissions? – Oded Oct 26 '12 at 21:12
  • 4
    Have you confirmed that `HttpContext.Current.Server.MapPath(@"~/Downloads/data.text")` resolves correctly to the path you need? – System Down Oct 26 '12 at 21:12
  • 5
    Is that supposed to be data.txt? – evanmcdonnal Oct 26 '12 at 21:13
  • Is your app pool configured to run under a security account that doesn't have access to this subfolder by any chance? – Daniel Graham Oct 26 '12 at 21:13
  • Did you already check out this SO question: http://stackoverflow.com/questions/2146652/streamreader-complains-that-file-does-not-exist-but-it-does – Jens Winter Oct 26 '12 at 21:14
  • @Oded If it's an error with the permission, the system will output "Access Denied" but not a `FileNotFoundException`. I believe that there's a problem with resolving `HttpContext.Current.Server.MapPath(@"~/Downloads/data.text")`. Have a great day :) – Picrofo Software Oct 26 '12 at 21:16
  • 1
    @Yatrix May you please show us the actual file path `data.text`? – Picrofo Software Oct 26 '12 at 21:17
  • @FantaMango77 I just checked it out. I also did a GetFiles and it displayed the file being there. – Yatrix Oct 26 '12 at 21:18
  • @PicrofoEGY \\kit-storage\users\myname\My Documents\Visual Studio 2010\WebSites\GridViewTest\Downloads\data.txt – Yatrix Oct 26 '12 at 21:19
  • @evanmcdonnal That's what I saved it as, yes. Why? – Yatrix Oct 26 '12 at 21:19
  • @Oded I would think I'd get an access related error for that. It's telling me the file's not there. Would that be a permission thing? – Yatrix Oct 26 '12 at 21:20
  • 1
    Because .text is not a common file extension where as .txt is and FileNotFoundExceptions are usually the result of pathing errors. – evanmcdonnal Oct 26 '12 at 21:21
  • @Oded I have full permissions on the directory, for the record. =) – Yatrix Oct 26 '12 at 21:23
  • @Yatrix I see that the file name is different from the code you posted. Your code represents the file path as follows: `HttpContext.Current.Server.MapPath(@"~/Downloads/data.text")` while the file is located at `\\kit-storage\users\myname\My Documents\Visual Studio 2010\WebSites\GridViewTest\Downloads\data.txt`. Perhaps removing '~\' and replacing '.text' with '.txt' would solve the problem. – Picrofo Software Oct 26 '12 at 21:23
  • @PicrofoEGY Dammit to hell!!! – Yatrix Oct 26 '12 at 21:24
  • @Yatrix I'm glad you had this issue resolved. Have a great day :) – Picrofo Software Oct 26 '12 at 21:26
  • 1
    @PicrofoEGY Worked like a charm. On that note, I'm going home. Thanks all. Sorry about that! – Yatrix Oct 26 '12 at 21:27

1 Answers1

3

Double-check the file name! In one of your comments you used the file name data.txt and not the name data.text. I suppose it's just a typo in your code.

Jens Winter
  • 144
  • 1
  • 9