How to get the current project name in C# code? How to get the current project name in C# code? asp.net asp.net

How to get the current project name in C# code?


You can use Assembly.GetCallingAssembly if you have your logging code in a separate library assembly, and call directly from your ASP.NET assembly to your library, and you mark the method so that it won't be inlined:

[MethodImpl(MethodImplOptions.NoInlining)]public static string JndGetEmailTextForDebuggingExceptionError(this Exception Ex){    StackFrame sf = Ex.JndGetStackFrame();    string OutputHTML =         "<i><b><u>For Developer Use Only: </u></b></i>"                    + "<br>" +                                                                                                      "<br>" +                                "Project Name:   "  + Assembly.GetCallingAssembly().GetName().Name + "<br>" +                                "File Name:      "  + sf.GetFileName()                             + "<br>" +                                "Class Name:     "  + sf.GetMethod().DeclaringType                 + "<br>" +                                "Method Name:    "  + sf.GetMethod()                               + "<br>" +                                "Line Number:    "  + sf.GetFileLineNumber()                       + "<br>" +                                "Line Column:    "  + sf.GetFileColumnNumber()                     + "<br>" +                                "Error Message:  "  + Ex.Message                                   + "<br>" +                                "Inner Message : "  + Ex.InnerException.Message                    + "<br>";    return OutputHTML;}

On any entry points in your library that can end up wanting to log the project name, you'd have to record the calling assembly and mark it NoInlining, then pass that around internally.

If you're using .NET 4.5, there's an alternative way to do this: CallerFilePath. It has the same restrictions on entry points, and it returns the source path on your machine instead of the assembly name (which is probably less useful), but it's easier to know that it'll work (because it compiles it, just like optional parameters are compiled in), and it allows inlining:

public static string JndGetEmailTextForDebuggingExceptionError              (this Exception Ex, [CallerFilePath] string filePath = ""){    StackFrame sf = Ex.JndGetStackFrame();    string OutputHTML =         "<i><b><u>For Developer Use Only: </u></b></i>" + "<br><br>" +                                "Source File Path:   "  + filePath + "<br>" +...


For anyone looking for an ASP.NET Core compatible solution, the following should work;

System.Reflection.Assembly.GetEntryAssembly().GetName().Name


.NET Core API Reference


This ought to be enough

string projectName = Assembly.GetCallingAssembly().GetName().Name;

Edit* If you are running this from another assembly then you should use GetCallingAssembly instead.