Get Application Path

c# entity-framework path winforms

Question

Ok, I am moving a few of my Winform projects from Framework 4.0 to Framework 4.5 and I am running into a problem trying to find the Executable path from a Class Library. I have an error logging class that I use to log errors here is a snippet of my code:

using System;
using System.IO;
using System.Windows.Forms;

namespace FunctionClassLibrary
{
    public static class ErrorLogging
    {
        public static string errLogFullPath
        {
            get
            {
                string errLogFile = "Application";
                m_errLogFullPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), errLogFile + "_ErrorLog.txt");
                return m_errLogFullPath;
            }
        }
    }
}

as you can see I am able to reference the System.Windows.Forms namespace which in turn exposes the Application.ExecutablePath property. Apparently in Framework 4.5 you can no longer reference System.Windows.Forms namespace. I have been googling all morning with no success, these two MSDN links: LINK1 and LINK2 both show examples using the System.Windows.Forms namespace. So my question is does anyone have a workaround for this issue?

**Update**

Ok, I am losing my mind, I just realized that I can simply use the Environment.GetFolderPath method to find the CommonProgramFiles folder, which is where I should be storing this file anyway. Sorry to bother everyone this morning.

1
3
10/6/2012 4:21:09 PM

Accepted Answer

You can use Directory.GetCurrentDirectory() or AppDomain.CurrentDomain.BaseDirectory instead:

Path.Combine(Directory.GetCurrentDirectory(), 
                 errLogFile + "_ErrorLog.txt");

Or:

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                 errLogFile + "_ErrorLog.txt");
5
10/6/2012 4:20:30 PM

Popular Answer

Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),errLogFile + "_ErrorLog.txt");


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow