2014-09-25 89 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。