2016-05-12 59 views
1

我有一些代碼來保存日誌我的FormClosing事件工作正常,直到我添加代碼來創建一個目錄,如果它不存在這樣的目錄。Winform沒有關閉爲什麼?

現在如果我添加註釋行應用程序不關閉。

我不明白爲什麼。

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    TmrReporte.Stop() 
    WriteRTBLog("Finalizacion del programa. - Version " & Me.ProductVersion, Color.Blue) 
    Dim Fecha As String 
    Fecha = Now.ToShortDateString & "-" & Now.ToLongTimeString 
    Fecha = Fecha.Replace("/", "") 
    Fecha = Fecha.Replace(":", "") 
    Dim PathArchivo As String = Application.StartupPath & "\Logs\" & Fecha & ".logout" 
    'If (Not System.IO.Directory.Exists(PathArchivo)) Then 
    ' System.IO.Directory.CreateDirectory(PathArchivo) 
    'End If 
    RTB_Log.SaveFile(PathArchivo, System.Windows.Forms.RichTextBoxStreamType.RichText)  
End Sub 
+1

大概一個異常被拋出 –

+1

logout擴展名的文件路徑http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-ex ception-message-in-a-winforms-application-on-a –

+0

@HansPassant所以在Windows x32中,我會得到一個錯誤,但在Windows x64上卻不行?有趣。 –

回答

2

問題是與目錄的字符串格式,您使用PathArchivo兩者的目錄和文件,而你實際上只需要在沒有文件名創建目錄的目錄的創建:

Dim PathDir As String = Application.StartupPath & "\Logs" 'use only directory string here 
Dim PathArchivo As String = Application.StartupPath & "\Logs\" & Fecha & ".logout" 'note that this is file name with .logout extension 
If (Not System.IO.Directory.Exists(PathDir)) Then 'creates directory without .logout 
    System.IO.Directory.CreateDirectory(PathDir) 
End If 
RTB_Log.SaveFile(PathArchivo, System.Windows.Forms.RichTextBoxStreamType.RichText)  

請注意,您PathArchivo

+0

是的!你是對的!這麼簡單,我感到羞愧。我想知道爲什麼我沒有錯誤? –

+0

太棒了,很高興它有幫助。也許是因爲它實際上是允許的 - 一個點號爲「。」的目錄;) – Ian