比如在C++中有__FILE__和__LINE__用来记录当前的文件和当前行数,不知道C#中是否有类似的东西记录当前的文件和当前的行数,如果还有方法能取出当前的函数是什么就更好了,谢谢

解决方案 »

  1.   

    StackTrace st = new StackTrace();
    StackFrame sf = st.GetFrame(0);
    Console.WriteLine("      {0}", sf.GetMethod());  
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  2.   

    try
    {
    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)
    {
    }
      

  3.   

    using System;
    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;
                }
            }
        }
    }
      

  4.   

    貌似好复杂的样子
    我试试,不知道谁还有简单一些的办法,为什么C#里面没有提供这样的功能的,向C++里面的__FILE__和__LINE__使用起来多方便啊
      

  5.   

    new   System.Diagnostics.StackFrame(true).GetFileLineNumber().ToString()   
      new   System.Diagnostics.StackFrame(true).GetFileName().ToString();
      

  6.   

    to hbxtlhx(平民百姓) 
    好像没有看懂我说的题意吧
      

  7.   

    new   System.Diagnostics.StackFrame(true).GetFileLineNumber().ToString()   
      new   System.Diagnostics.StackFrame(true).GetFileName().ToString();
    --------------
    据说这个只在dubug版本下有用,在release下面不行,是吗?