Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm running into a problem while trying to read from a text file I have stored in my _layouts/myaspapp folder. I've tried everything I've been able to find on the web, but will always either get a File Not Found error (while trying to use StreamReader or something similar or a 401 Unauthorized (using HttpWebRequest/Response).

I'm out of ideas on how to access this file and read the text from it. I am able to see the text file itself if I go to http://server/_layouts/myaspapp/text.txt, but I have not been able to open it and read it from C#.

Any tips or suggestions would be much appreciated.

share|improve this question
    
A quick question: why read it through the IIS and not directly from the file system? –  Wictor Wilen MCA MCM MVP Jul 20 '10 at 22:12
    
Disregard from my comment if you are reading it from outside of Sharepoint... –  Wictor Wilen MCA MCM MVP Jul 20 '10 at 22:13

1 Answer 1

up vote 1 down vote accepted

This has worked for me:

string contents = string.Empty;    
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    contents = reader.ReadToEnd();
}
response.Close();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.