比如在C++中有__FILE__和__LINE__用来记录当前的文件和当前行数,不知道C#中是否有类似的东西记录当前的文件和当前的行数,如果还有方法能取出当前的函数是什么就更好了,谢谢
解决方案 »
- 向数据库中更新信息问题
- 用C#做的文件管理系统如何获取文件夹路径
- 怎么把上一页中在dropdownlist中得到的值绑定在当前页dropdownlist中显示的位置排在第一个
- 写了一Windform程序遇到关于dataGridView绑定问题.急急急急急急急
- VS2008在2036年终使用,仅可使用28年?
- sqlconnection的Open为什么会提示错误
- 求工资数据库结构
- 有没有人提供这样的组件?100分相送,不够再加!
- 如何让tabcontrol 的边界不是3D风格,而是平面风格?
- 请问有没有人用C#做项目,请谈谈C#的具体感受
- 在IE中嵌入Windows控件这样做程序,有什么局限性?
- 怎样判断结束日期在开始日期之后?
StackFrame sf = st.GetFrame(0);
Console.WriteLine(" {0}", sf.GetMethod());
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
{
using (StreamReader sr = new StreamReader("txt.txt"))
{
String line;
int i = 0;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine("第{0}行", i);
i++;
}
}
}
catch (Exception e)
{
}
using System.Diagnostics;class StackTraceSample
{
[STAThread]
static void Main(string[] args)
{
StackTraceSample sample = new StackTraceSample();
try
{
sample.MyPublicMethod();
}
catch (Exception)
{
// Create a StackTrace that captures
// filename, line number, and column
// information for the current thread.
StackTrace st = new StackTrace(true);
for(int i =0; i< st.FrameCount; i++ )
{
// Note that high up the call stack, there is only
// one stack frame.
StackFrame sf = st.GetFrame(i);
Console.WriteLine();
Console.WriteLine("High up the call stack, Method: {0}",
sf.GetMethod()); Console.WriteLine("High up the call stack, Line Number: {0}",
sf.GetFileLineNumber());
}
}
} public void MyPublicMethod ()
{
MyProtectedMethod();
} protected void MyProtectedMethod ()
{
MyInternalClass mic = new MyInternalClass();
mic.ThrowsException();
} class MyInternalClass
{
public void ThrowsException()
{
try
{
throw new Exception("A problem was encountered.");
}
catch (Exception e)
{
// Create a StackTrace that captures filename,
// line number and column information.
StackTrace st = new StackTrace(true);
string stackIndent = "";
for(int i =0; i< st.FrameCount; i++ )
{
// Note that at this level, there are four
// stack frames, one for each method invocation.
StackFrame sf = st.GetFrame(i);
Console.WriteLine();
Console.WriteLine(stackIndent + " Method: {0}",
sf.GetMethod() );
Console.WriteLine( stackIndent + " File: {0}",
sf.GetFileName());
Console.WriteLine( stackIndent + " Line Number: {0}",
sf.GetFileLineNumber());
stackIndent += " ";
}
throw e;
}
}
}
}
我试试,不知道谁还有简单一些的办法,为什么C#里面没有提供这样的功能的,向C++里面的__FILE__和__LINE__使用起来多方便啊
new System.Diagnostics.StackFrame(true).GetFileName().ToString();
好像没有看懂我说的题意吧
new System.Diagnostics.StackFrame(true).GetFileName().ToString();
--------------
据说这个只在dubug版本下有用,在release下面不行,是吗?