2014-09-25 87 views
0

我有以下功能讀取JPG格式文件的錄製日期:讀取元數據文件鎖定

Public Shared Function GetRecordingDateOfPhoto(pathOfPhoto As String) As DateTime 
      If Not IO.File.Exists(pathOfPhoto) Then 
       Throw New FileNotFoundException 
      End If 
      Dim bitmapSource As BitmapSource = BitmapFrame.Create(New Uri(pathOfPhoto, UriKind.Relative)) 
      Dim bitmapMetadata As BitmapMetadata = TryCast(bitmapSource.Metadata, BitmapMetadata) 
      Dim result As DateTime 
      If DateTime.TryParse(bitmapMetadata.DateTaken, result) Then 
       Return result 
      Else 
       Throw New FormatException 
      End If 
     End Function 

這個函數返回正確的日期,但是當我做這樣的事情

dim dateOfPhoto as Date = GetRecordingDateOfPhoto("foo.jpg") 
My.Computer.FileSystem.MoveFile("foo.jpg", "bar.jpg") 

然後我得到MoveFile異常(...):IOException異常

什麼我必須明確的改變(「進程不能因爲它是被其它進程使用的訪問文件」)(也許全光照g/end using?)在GetRecordingDateOfPhoto(...) - 函數中避免這種異常?

非常感謝提前。

回答

0

發生這種情況是因爲該功能是共享的。無論有多少類實例,它的局部變量只存在一次。嘗試使其不共享或將您的變量設置爲Nothing。