IO.FileNotFoundException 但文件应该存在

发布于 2024-11-28 00:37:36 字数 2195 浏览 1 评论 0原文

尽管我确信该文件存在,但我收到了令人惊讶的 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 对象的引用。

这是Log类中的WriteInfoLogFileIO.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 解决方案刷新很简单。从来没有听说过这个函数,奇怪的是当我不刷新文件时我得到一个 FileNotFoundException 。

Me.Log.LogFile.Refresh()

I'm getting a suprising FileNotFoundException although i'm sure that the file exists.

I simply wanted to add Logfiles(IO.FileInfo) as attachments to an email, therefore i tried to check the length of every file to detect if they must be added/zipped.
This works fine if these files already exist.
But if i've created them in this run, i get above exception when i try to check the length. It's oddly enough that i can write into these "not existing" files(actually FileInfo.Exists returns false) without a problem one line before.

Here is some code...

Creating one of the files in the constructor of a class named 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

I can write into the file without a problem:

Me.Log.WriteInfo("BlahBlahBlah...", False)

One line after i'm getting the exception on LogFile.Length:

If Me.Log.LogFile.Length <> 0 Then
    files.Add(Me.Log.LogFile)
End If

Me.Log is a custom logging-class object named Log that holds the reference to the FileInfo object.

This is WriteInfo in class Log, LogFile is the 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

Actually @ShellShocks solution with Refresh was simple. Never heard of this function, strange that i get a FileNotFoundException when i don't refresh the file.

Me.Log.LogFile.Refresh()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-12-05 00:37:36

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

Try calling FileInfo.Refresh before FileInfo.Exists, or FileInfo.Length--these properties may be cached, so Refresh will get the latest value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文