using reflection , or any means, how can I get the name of a source code file at runtime?

using reflection ,or any means, how can I get the name of a source code  file at runtime?
LVL 1
HLRosenbergerAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Which source code file? The one from which the executing method comes, or something else?

You can get the source file of the executing method using:
System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(true);
string executingFile = stack.GetFrame(0).GetFileName();

Open in new window

0
 
käµfm³d 👽Commented:
What version of .NET and/or Visual Studio are you using?
0
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
For this to work, you need to make sure that you "Generate debug info" in the Advanced Compile Options (Compile tab of the project's Properties window) and that you distribute the .pdb file that is generated along with the application.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
Hi HLRosenberger,

I use the following to determine exact source of error using system.reflection.methodbase()
      Try
        ' ...

      Catch ex As Exception
        Dim sf As System.Diagnostics.StackFrame = New System.Diagnostics.StackFrame()
        Dim mb As System.Reflection.MethodBase = sf.GetMethod()
        Session("ErrorMessage") = "Error in " _
          & Context.Request.Url.ToString().Replace(Context.Request.Url.GetLeftPart(UriPartial.Authority) & VirtualPathUtility.ToAbsolute("~/"), "~/") _
          & "." & mb.Name _
          & "<br />" & ex.Message _
          & "<br />" & ex.Source
      End Try

Open in new window

Respectfully yours,
Alan
0
 
HLRosenbergerAuthor Commented:
thanks
0
 
käµfm³d 👽Commented:
I see my question was completely ignored. I asked it for a reason. If you're working under .NET 4.5, then you might investigate The CallerFilePathAttribute class.
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.