虽然我确信该文件存在,但我得到了令人惊讶的FileNotFoundException.
原文链接:https://www.f2er.com/vb/255295.html我只是想添加Logfiles(IO.FileInfo)作为电子邮件的附件,因此我试图检查每个文件的长度,以检测它们是否必须添加/压缩.
如果这些文件已存在,这可以正常工作.
但是,如果我在这次运行中创建它们,当我尝试检查长度时,我会遇到异常.奇怪的是,我可以写入这些“不存在的”文件(实际上FileInfo.Exists返回false),之前没有问题.
这是一些代码……
Me.LogFile = New IO.FileInfo(infoLogPath) If Not LogFile.Exists() Then 'tried to use `Using` on the Stream but that doesn't change anything' Using stream = Me.LogFile.Create() 'close and dispose implicitely End Using End If
我可以毫无问题地写入文件:
Me.Log.WriteInfo("BlahBlahBlah...",False)
我在LogFile.Length上获得异常后的一行:
If Me.Log.LogFile.Length <> 0 Then files.Add(Me.Log.LogFile) End If
Me.Log是一个名为Log的自定义日志记录类对象,它保存对FileInfo对象的引用.
这是类Log中的WriteInfo,LogFile是IO.FileInfo-onject:
Public Sub WriteInfo(ByVal message As String,ByVal finishLog As Boolean) Try Using w As IO.StreamWriter = Me.LogFile.AppendText If Me.WithTimestamp Then w.WriteLine(Date.Now.ToString(Globalization.CultureInfo.InvariantCulture) & ": " & message) Else w.WriteLine(message) End If If finishLog Then w.WriteLine("__________________________") w.Flush() w.Close() End Using Catch writeLogException As Exception Try WriteError(writeLogException,True) Catch innerEx As Exception 'ignore End Try End Try End Sub
实际上@ShellShocks solution与Refresh很简单.从来没有听说过这个函数,奇怪的是,当我不刷新文件时,我得到了一个FileNotFoundException.
Me.Log.LogFile.Refresh()