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

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
 
M

[MSFT]

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.)
 
T

TC

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
 
J

jessica

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top