0

Hey guys,

I would like to read the contents of a file that resides the same directory as my .aspx file. In my C# codebehind, I don't seem to be able to get the correct path. I have tried numerous methods like using "./branch.txt", "branch.txt", "/branch.txt" and even tried using HttpContext.Current.Request.Url.AbsolutePath. etc etc.

Can anyone give me any tips on how to do this please?

I have the folowing code at the moment...

String url = HttpContext.Current.Request.Url.AbsolutePath;
            error.InnerHtml += url;
            try {
                StreamReader sr = new StreamReader("~/branch.txt");
                BRANCHID = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex) {
                error.InnerHtml += "----------Branch Parsing Exception----------<br>" + ex.Message;
                BRANCHID = "17";
            }

Please note that "error" is just a placeholder div where I am outputting my results to test.

Cheers.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by bops
0

Nevermind guys, I have solved the issue. For anyone else with a similar problem I used the following...

Server.MapPath("branch.txt")

Cheers.

0

Try using Server.MapPath, especially if you're in a hosted environment. If you're not hosted, and running on a machine that you have admin rights to, you can create a shared directory, and connected to it using a hard coded absolute path to it's UNC path. (Ex. "\\ServerName\SharedDirectory"

0

Yeah, I've been trying to type my response and get work done at the same time... sorry for the double info.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.