我檢查文件是否存在File.Exists(filePath)
。然後我嘗試從Excel.Workbooks.OpenText(filePath)
的Excel內打開文件。但Excel抱怨文件不在那裏。有沒有搞錯?VB.Net File.Exists()返回True,但Excel無法打開
上下文是,我退出到另一個應用程序來處理給定的文件並生成.out文件,然後我將其轉換爲Excel工作簿。
'' At this point, filePath is a .txt file.
Dim args As String = String.Format("""{0}""", filePath)
...
Dim exe As String = Config.ExtractEXE
Dim i As New ProcessStartInfo(exe)
i.Arguments = args
Dim p As Process = Process.Start(i)
p.WaitForExit()
...
'' filePath now becomes the .out file.
'' Then eventually, I get around to checking:
'If Not File.Exists(filePath) Then
' MsgBox("Please ensure...")
' Exit Sub
'End If
'' In response to an answer, I no longer check for the existence of the file, but
'' instead try to open the file.
Private Function fileIsReady(filePath As String) As Boolean
Try
Using fs As FileStream = File.OpenRead(filePath)
Return True
End Using
Catch
Return False
End Try
End Function
Do Until fileIsReady(filePath)
'' Wait.
Loop
ExcelFile.Convert(filePath...)
'' Wherein I make the call to:
Excel.Workbooks.OpenText(filePath...)
'' Which fails because filePath can't be found.
是否存在延遲問題,例如.Net在其他應用程序可以訪問之前識別文件的存在?我只是不明白爲什麼File.Exists()
可以告訴我該文件在那裏,然後Excel無法找到它。
據我所知,唯一可能打開該文件的應用程序就是我打電話進行處理的應用程序。但是該應用程序應該在文件到達p.WaitForExit()
時完成,對嗎?
我不得不將這個應用程序作爲一個已知的bug部署,這真的很糟糕。對用戶來說有一個簡單的解決方法。但仍然 - 這個錯誤不應該。希望你能幫助。
此時該文件是否被另一個應用程序打開?例如。一個獨立的Excel? –
@Trekstuff,請參閱我在問題中添加的評論以迴應您的問題。謝謝! –