Error: Could not find a part of the path / Reading and Writing to files in ASP.Net

Error: Could not find a part of the path / Reading and Writing to files in ASP.Net

Post by TC » Thu, 06 May 2004 05:53:36


Hello,

I'm trying to read a text file located in the top folder of the virtual
directory and I'm receiving the following error:

"Could not find a part of the path"

Below is the code that I'm using:

string MyFile;
MyFile =Request.ApplicationPath + "//sample1.txt";
StreamReader srReader;
srReader = new StreamReader(MyFile);
txtMessage.Text = srReader.ReadLine();

I have tried both "Windows" and "None" for "Authentication mode".

Any ideas?

Thanks & Regards,

TC
 
 
 

Error: Could not find a part of the path / Reading and Writing to files in ASP.Net

Post by lukezha » Thu, 06 May 2004 11:19:40

Hello,

Request.ApplicationPath will return a virtual path string like
"/WebApplication1". However, StreamReader require a physical path like
"C:\inetpub\webapplication1\sample.txt". To convert a virtual path to
physical path, we can use MapPath method:

MyFile =Server.MapPath(Request.ApplicationPath + "//sample1.txt");

Hope this help,

Luke
Microsoft Online Support

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