2016-05-12 60 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

相关问题