我想檢查C:\ Data.xlsb是否已經打開。有沒有一種方法可以檢查文件是否已經打開?
我有下面的代碼從這裏How to tell if a certain Excel file is open using VB.NET?
Public Shared Function OpenUnlockedFile(ByVal path As String) As StreamWriter
Dim sw As StreamWriter = nothing
Try
sw = New StreamWriter(path)
Catch ex As IOException When System.Runtime.InteropServices.Marshal.GetLastWin32Error() = 32
REM locked, return nothing
End Try
Return sw
End Function
但我不知道如何使用上面的代碼。
我更喜歡sub而不是功能。
此致敬禮。
你爲什麼喜歡Sub over Function?如果你想檢查文件是否被打開,函數會更好,因爲你可以使它返回True或False,因此你可以在'If'語句中檢查它。 –
我建議你將返回類型修改爲'Boolean',並在函數的最後一行返回'True',並在'Catch'塊中返回False。我也認爲'FileStream'會比'StreamWriter'更好。 –
@VisualVincent您可以發佈您建議的代碼嗎? – Markowitz