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.

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

Server.MapPath("branch.txt")

Cheers.

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"

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

Not a problem. Thanks for your response anyway!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.