http://blog.csdn.net/newegg2009/article/details/6220385
C#获得当前执行的函数名、当前代码行、源代码文件名
得到函数名:
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());
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));
}
}
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());
}
{
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;
}
{
dirinfo.Attributes = FileAttributes.Normal;
}
//取得访问控制列表
DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
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);
}
{
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);
}
}
}
{
// 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);
}
}
}