by Svetlozar Angelov
7. January 2010 11:31
Here is an example how you can get the line number of source file programmatically:
static void Main(string[] args)
{
int s = GetLine();
Console.WriteLine(s.ToString());
}
static int GetLine()
{
StackFrame stackFrame = new StackFrame(1, true);
return stackFrame.GetFileLineNumber();
}
StrackFrame constructor has 6 overloads, we need the StackFrame(int skipFrames,bool fNeedFileInfo) one to skip 1 frame to get to the calling function frame and true(fNeedFileInfo) to capture the file name, line number, and column number of the stack frame.
StackFrame.GetFileLineNumber() gets the line number in the file that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.