2013-10-08 67 views
0

我正嘗試使用下面的代碼來嘗試關閉文件流,因爲即使在處理它仍然在服務器上使用它之後,我仍然收到錯誤。所以我想也許循環它直到它關閉,因爲它不會在1次嘗試後關閉。無論如何,我收到FileMode,File Access,FileShare不是IO成員的錯誤。Filemode不是io的成員

Dim thisFileInUse As Boolean = False 
    Dim fs As System.IO.FileStream 

    If System.IO.File.Exists(sFile) Then 
     Try 
      fs = System.IO.File.Open(sFile, io.FileMode.Open, io.FileAccess.Write, io.FileShare.ReadWrite) 
      fs.Close() 
      fs.Dispose() 
     Catch 
      Try 
       fs.Close() 
       fs.Dispose() 
      Catch 

      End Try 

      thisFileInUse = True 
     End Try 
    End If 
    Return thisFileInUse 

回答

1

試試這個:

Imports System.IO 


Dim thisFileInUse As Boolean = False 
Dim fs As FileStream 

If File.Exists(sFile) Then 
    Try 
     fs = File.Open(sFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite) 
... 

System.IO是命名空間的名稱,如果導入它,你可不必一遍遍輸入。

+0

謝謝你的工作 – user1342164

1

使用Finally和正確的命名空間System.IO

Imports System.IO 
' .... 

Public Shared Function IsFileInUse(sFile As String) As Boolean 
    Dim stream As FileStream = Nothing 
    Dim isInUse As Boolean = False 
    Try 
     stream = File.Open(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None) 
    Catch generatedExceptionName As IOException 
     isInUse = True 
    Finally 
     If stream IsNot Nothing Then 
      stream.Close() 
     End If 
    End Try 
    Return isInUse 
End Function 
相關問題