Alternative to using Page.Request as a way to get PhysicalApplicationPath

Sample path to folder:

D:\all testing websites go here\test_piratewear\debuglog

Right now my code is using Page.Request (passed in) in order to get the above path.

Is there an alternative to using Page.  Sometimes I can't pass in Page.Request because Request is not available.

Current code:

 public static void SaveOffDebugInfo(HttpRequest hr, string filenameappend)
        {
            string tempGUID = Guid.NewGuid().ToString();

            FileStream fs = null;
                  

            fs = new FileStream(hr.PhysicalApplicationPath + "debuglog\\" + filenameappend + "_" + hr.UserHostAddress + "__" + tempGUID + "_DEBUG.txt", FileMode.Create);

            StreamWriter sw = new StreamWriter(fs);


            foreach (string s in debuglogentry)
            {
                sw.WriteLine(s);
            }


            sw.Flush();

            sw.Close();
        }

Open in new window

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Miguel OzConnect With a Mentor Software EngineerCommented:
If this path does not change, you can add it to your web config file and read this value every time you need it.
0
 
Tom KnowltonWeb developerAuthor Commented:
The path does change each time.

My eventual solution:

  public static void SaveOffDebugInfo(string filenameappend)
        {
            string tempGUID = Guid.NewGuid().ToString();

            FileStream fs = null;

            string path = HttpRuntime.AppDomainAppPath;

            string ip = HttpContext.Current.Request.UserHostAddress;


            fs = new FileStream(path + "debuglog\\" + filenameappend + "_" + ip + "__" + tempGUID + "_DEBUG.txt", FileMode.Create);

            StreamWriter sw = new StreamWriter(fs);


            foreach (string s in debuglogentry)
            {
                sw.WriteLine(s);
            }


            sw.Flush();

            sw.Close();
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.