gpt4 book ai didi

.net - IO.FileNotFoundException 但文件应该存在

转载 作者:行者123 更新时间:2023-12-04 12:12:42 26 4
gpt4 key购买 nike

我得到了一个惊喜FileNotFoundException虽然我确定该文件存在。

我只是想添加日志文件(IO.FileInfo)作为电子邮件的附件,因此我尝试检查每个文件的长度以检测是否必须添加/压缩它们。
如果这些文件已经存在 就可以了.
但是如果我在这次运行中创建了它们,当我尝试检查长度时我会遇到异常。奇怪的是,我可以在前一行毫无问题地写入这些“不存在”的文件(实际上 FileInfo.Exists 返回 false )。

这是一些代码...

在名为 Log 的类的构造函数中创建文件之一:

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 的引用目的。

这是 WriteInfo在类里面 Log , LogFileIO.FileInfo -项目:
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 solutionRefresh很简单。从来没有听说过这个功能,奇怪的是当我不刷新文件时我得到一个 FileNotFoundException 。
Me.Log.LogFile.Refresh()

最佳答案

尝试调用FileInfo.Refresh在 FileInfo.Exists 或 FileInfo.Length 之前——这些属性可能会被缓存,因此 Refresh 将获取最新值。

关于.net - IO.FileNotFoundException 但文件应该存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com