2015-11-22 89 views
-1

有沒有辦法從StackTrace中提取僅在C#中拋出異常時生成的短文件名?在C#Stacktrace中獲取短文件名(帶擴展名)?

例如,我有這樣的代碼,其捕捉拋出的異常,並顯示一個JavaScript警告:

try 
{ 
    da.saveDebitCardPaymentDetails(cboDebitCardProvider.SelectedItem.Text, txtDcNum.Text, txtDcHolderName.Text, cboDdlDcard.SelectedItem.Text, 1 /*kasi test lang muna*/, 37 /*kasi test lang muna*/, 10000 /*kasi test lang muna*/); 
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "test", "alert('Your payment has been processed.');", true); 
} 
catch (Exception ee) 
{ 
    string bugToTrace = (new StackTrace(ee, true)).GetFrame(0).GetFileName() + " at line " + (new StackTrace(ee, true)).GetFrame(0).GetFileLineNumber().ToString(); 
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "done", "alert('Oops. Something has gone wrong. Please try again in a few moments. Cause of error: " + ee.Message.Replace("'", "") + " (" + bugToTrace + ")" + "');", true); 
} 

當拋出異常,並且捕捉,被顯示的完整路徑(沒有反斜槓)。我希望它只顯示,例如:AClass.cs at line 1909

編輯:這就是我說的。 enter image description here

+0

有使用'/'和使用最後的值或搜索的最後的indexOf考慮拆分'/'並使用子字符串? – Rajesh

+0

你是什麼意思_「沒有反斜槓」_?源文件信息(如果存在)使用反斜槓分隔路徑部分;在什麼情況下你會看到沒有分隔符的路徑部分?你有沒有嘗試過'Path.GetFileName()'?你有沒有試過_anything_?請提供[一個很好的,_minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve),其中顯示了您已經嘗試過的內容,並精確地解釋了該代碼的作用以及與想要的不同之處。 –

+0

另請注意,在Windows環境中,短語「短文件名」的含義與您在這裏所要求的不同。即它意味着一個8.3格式的文件名,而不是一個完整的「長」文件名,該文件名在'.'和/或'.'之後的部分中的三個以上的字符之前有超過八個字符。 –

回答