Framework C#获得当前执行的函数名、当前代码行、源代码文件名

C#获得当前执行的函数名、当前代码行、源代码文件名

[日期:2010-10-18 11:40]   来源:.NET中国学习网  作者:admin   [字体: ]
  得到函数名:
  System.Diagnostics.StackTrace  st  =  new  System.Diagnostics.StackTrace();
  this.Text  =  st.GetFrame(0).ToString();
  得到代码行,源代码文件名:
  StackTrace st = new StackTrace(new StackFrame(true));
  Console.WriteLine(" Stack trace for current level: {0}", st.ToString());
  StackFrame sf = st.GetFrame(0);
  Console.WriteLine(" File: {0}", sf.GetFileName());
  Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
  Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
  Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
using System.Diagnostics;
http://madskristensen.net/post/JavaScript-AlertShow(e2809dmessagee2809d)-from-ASPNET-code-behind.aspx
private void btnException_Click(object sender, System.EventArgs e)
{

try
{
ProcException1(1, 2);
}
catch(Exception exp)
{
GetFullStackFrameInfo(new StackTrace(exp));
}
}
void btnSave_Click(object sender, EventArgs e)
{
   try
   {
      SaveSomething();
      Alert.Show("You document has been saved");
   }
   catch (ReadOnlyException)
   {
      Alert.Show("You do not have write permission to this file");
   }
}

private void btnStackTrace_Click(object sender, System.EventArgs e)
{
int x = 2;
ProcA(1, ref x, "Hello");
}

private void GetFullStackFrameInfo(StackTrace st)
{
int fc = st.FrameCount;

lstStackItems.Items.Clear();

for(int i = 0; i < fc - 1; i++)
{
lstStackItems.Items.Add(GetStackFrameInfo(st.GetFrame(i)));
}
}

private string GetStackFrameInfo(StackFrame sf)
{
string strParams;

MethodInfo mi;
Type typ;
string strOut = string.Empty;

mi = (MethodInfo) sf.GetMethod();

if (mi.IsPrivate)
{
strOut += "private ";
}
else if ( mi.IsPublic )
{
strOut += "public ";
}
else if ( mi.IsFamily )
{
strOut += "protected ";
}
else if ( mi.IsAssembly )
{
strOut += "internal ";
}

if ( mi.IsStatic )
{
strOut += "static ";
}

strOut += mi.Name + "(";

ParameterInfo[] piList = sf.GetMethod().GetParameters();

strParams = string.Empty;

foreach(ParameterInfo pi in piList)
{
strParams += string.Format(", {0} {1} {2}", ((pi.ParameterType.IsByRef) ? "ByRef" : "ByVal"), pi.Name, pi.ParameterType.Name);
}

if (strParams.Length > 2)
{
strOut += strParams.Substring(2);
}
typ = mi.ReturnType;
strOut += ") " + typ.ToString();

return strOut;
}
private void ProcException1(int x, int y)
{
ProcException2("Mike", 12);
}

private void ProcException2(string Name, long Size)
{
ProcException3();
}

private string ProcException3()
{
return ProcException4("mike@microsoft.com");
}

private string ProcException4(string EmailAddress)
{
throw new ArgumentException("This is a fake exception!");
}

private void ProcA(int Item1, ref int Item2, string Item3)
{
ProcB(string.Concat(Item1, Item2, Item3));
}

private void ProcB(string Name)
{
GetFullStackFrameInfo(new StackTrace());
}
        private static void addpathPower(string pathname, string username, string power)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(pathname);
            if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
            {
                dirinfo.Attributes = FileAttributes.Normal;
            }
            //取得访问控制列表  
            DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
            switch (power)
            {
                case "FullControl":
                    dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                    break;
                case "ReadOnly":
                    dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
                    break;
                case "Write":
                    dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
                    break;
                case "Modify":
                    dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
                    break;
            }
            dirinfo.SetAccessControl(dirsecurity);
        }
public static void Show(string message)
{
   // Cleans the message to allow single quotation marks

   string cleanMessage = message.Replace("'", "//'"
);
   string script = "<script type=/"text/javascript/">alert('" + cleanMessage + "');</script>"
;

   // Gets the executing web page

   Page page
= HttpContext.Current.CurrentHandler as
Page;

   // Checks if the handler is a Page and that the script isn't allready on the Page

   if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"
))
   {
      page.ClientScript.RegisterClientScriptBlock(
typeof(Alert), "alert"
, script);
   }
}    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是针对 C# 读写邮件文件的每代码的详细注释讲解。 首先,需要引用以下命名空间: ```csharp using System.IO; using System.Net.Mail; using System.Text; ``` 接着,可以通过以下代码读取邮件文件的内容: ```csharp string mailFilePath = "邮件文件路径"; string mailContent = File.ReadAllText(mailFilePath, Encoding.UTF8); ``` 其中,`mailFilePath` 是邮件文件的路径,`Encoding.UTF8` 是指定编码方式为 UTF-8。 然后,可以通过以下代码将邮件内容解析为 MailMessage 对象: ```csharp MailMessage mailMessage = new MailMessage(); using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(mailContent))) { mailMessage = new MailMessage(new StreamReader(stream).ReadLine()); } ``` 其中,`MailMessage` 是 .NET Framework 中用于表示电子邮件的类。 接着,可以通过以下代码获取邮件的各个部分: ```csharp foreach (var part in mailMessage.AlternateViews) { if (part.ContentType.MediaType == "text/html") { string htmlBody = Encoding.UTF8.GetString(part.ContentStream.ToArray()); // 处理 HTML 正文的代码 } else if (part.ContentType.MediaType == "text/plain") { string textBody = Encoding.UTF8.GetString(part.ContentStream.ToArray()); // 处理纯文本正文的代码 } } foreach (var attachment in mailMessage.Attachments) { // 处理附件的代码 } ``` 最后,需要释放 MailMessage 对象: ```csharp mailMessage.Dispose(); ``` 以上就是 C# 读写邮件文件的每代码的详细注释讲解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值