How to get the current line number?

Here is an example of what I want to do:

MessageBox.Show("Error line number "+CurrentLineNumber); 

The current line number will be the line number in the source code of this code fragment.

How can i do this?

+99
c # line-numbers wpf
Sep 23 '12 at 22:23
source share
7 answers

In .NET 4.5 / C # 5, you can get the compiler to do this job for you by writing a utility method that uses the new caller attributes:

 static void SomeMethodSomewhere() { ShowMessage("Boo"); } ... static void ShowMessage(string message, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")"); } 

This will display, for example:

Boo on line 39 (SomeMethodSomeewhere)

There is also [CallerFilePath] , which tells you the path to the source code file.

+146
Jan 02 '13 at 13:17
source share

Use the StackFrame.GetFileLineNumber method, for example:

 private static void ReportError(string message) { StackFrame callStack = new StackFrame(1, true); MessageBox.Show("Error: " + message + ", File: " + callStack.GetFileName() + ", Line: " + callStack.GetFileLineNumber()); } 

See Scott Hanselman's Blog Entry for more information.

[Edit: Added the following]

For those using .Net 4.5 or later , consider the CallerFilePath , CallerMethodName, and CallerLineNumber attributes in the System.Runtime.CompilerServices namespace. For example:

 public void TraceMessage(string message, [CallerMemberName] string callingMethod = "", [CallerFilePath] string callingFilePath = "", [CallerLineNumber] int callingFileLineNumber = 0) { // Write out message } 

The arguments must be string for CallerMemberName and CallerFilePath and int for CallerLineNumber and must have a default value. Specifying these attributes in the method parameters tells the compiler to insert the corresponding value into the calling code at compile time, which means that it works through obfuscation. See Caller Information for more information.

+60
Sep 23 '12 at 22:26
source share

I prefer one liner like this:

 int lineNumber = (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber(); 
+17
May 29 '13 at 15:20
source share

For those who need a .NET 4.0+ solution:

 using System; using System.IO; using System.Diagnostics; public static void Log(string message) { StackFrame stackFrame = new System.Diagnostics.StackTrace(1).GetFrame(1); string fileName = stackFrame.GetFileName(); string methodName = stackFrame.GetMethod().ToString(); int lineNumber = stackFrame.GetFileLineNumber(); Console.WriteLine("{0}({1}:{2})\n{3}", methodName, Path.GetFileName(fileName), lineNumber, message); } 

How to call:

 void Test() { Log("Look here!"); } 

Output:

Void Test () (FILENAME.cs: 104)

Look at here!

Change the format of Console.WriteLine as you like!

+4
Jun 25 '14 at 17:47
source share

If it is in a catch try block, use this.

 try { //Do something } catch (Exception ex) { System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true); Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber()); } 
+3
Sep 23 '12 at 22:28
source share

In .NET 4.5, you can get the line number by creating a function:

 static int LineNumber([System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) { return lineNumber; } 

Then every time you call LineNumber() , you will have the current line. This has the advantage over any solution using StackTrace that it should work both in debugging and in release.

So, taking the initial request of what is required, it will become the following:

 MessageBox.Show("Error enter code here line number " + LineNumber()); 

It builds on Mark Gravell's excellent answer.

+1
May 23 '17 at 15:17
source share

This works for me:

 try { //your code; } catch(Exception ex) { MessageBox.Show(ex.StackTrace + " ---This is your line number, bro' :)", ex.Message); } 
-7
Jan 11 '14 at 23:26
source share



All Articles