被人抢先了不过还是详细说一下吧: static void Main(string[] args) { try { int t = 1; int i = 5 / --t; } catch (Exception e) { int i = e.StackTrace.IndexOf("行号"); string s = e.StackTrace.Substring(i+3); i = s.IndexOf(' '); if (i != -1) { s = s.Substring(0, i); } //这里s已经存储了错误所在行号 } }
try { int t = 1; int i = 5 / --t; } catch (Exception e1) { System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(e1,true); int i= st.GetFrame(0).GetFileLineNumber(); int j = st.GetFrame(0).GetFileColumnNumber(); string k = st.GetFrame(0).GetFileName(); }
StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(st.ToString()); Console.WriteLine(st.GetFrame(0).GetFileLineNumber().ToString());
try
{
throw new Exception();
}
catch (Exception e)
{ Console.WriteLine(e.StackTrace);
}
static void Main(string[] args)
{
try
{
int t = 1;
int i = 5 / --t;
}
catch (Exception e)
{
int i = e.StackTrace.IndexOf("行号");
string s = e.StackTrace.Substring(i+3);
i = s.IndexOf(' ');
if (i != -1)
{
s = s.Substring(0, i);
}
//这里s已经存储了错误所在行号
}
}
不要截取字符串 根据VS版本会有不一样的显示
同意这位的意思, 如果是en版的.net 则需要截获:line的位置
{
int t = 1;
int i = 5 / --t;
}
catch (Exception e1)
{ System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(e1,true);
int i= st.GetFrame(0).GetFileLineNumber();
int j = st.GetFrame(0).GetFileColumnNumber();
string k = st.GetFrame(0).GetFileName();
}
Console.WriteLine(st.ToString());
Console.WriteLine(st.GetFrame(0).GetFileLineNumber().ToString());
还没有怎么注意过呢,学习了