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

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

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
 
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.)
 
Hey Luke,

Yep! It worked! DOH!

Ironically, I saw this method in some documentation and it was the 1 thing
that I didn't try :-(

Thanks Again,

TC
 
Hi, I got a similar problem just now. On my local machine I mapped th
V:\LoggerNet to a remote server folder where the real-time data com
in. I checked the file, it gets updated with the original serve
source. The code is like

streamreader sr=null;
sr = File.OpenText("V:\\LoggerNet\\newdata.dat");
text = sr.ReadToEnd();

But when I run the code, it gave error "Could not find a part of th
path "V:\LoggerNet\newdata.dat" on my webpage.

Do you have any idea why this happen?? Thanks a lot!

Jessica


*Hello,

Request.ApplicationPath will return a virtual path string like
"/WebApplication1". However, StreamReader require a physical pat
like
"C:\inetpub\webapplication1\sample.txt". To convert a virtual pat
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 confer
no
rights.)


-
jessic
 
Back
Top