locked
The process cannot access the file because it is being used by another process

    Question

  • I have files that have a unc path with a filename entered into the comments of the file and I would like to be able to simply read the comments of a given file and if the comments contain a legit unc path then move the file to the location given.

    When I run this code...

    Public Sub remigrate(ByVal source As String)

            Dim returnPath As String
            returnPath = OrigPath(source.ToString)


            If returnPath.ToString.Contains("\\") = True Then
                System.IO.File.Move(source.ToString, returnPath)
            End If
    End Sub
       
    Private Function OrigPath(ByVal file As String) As String
            Dim FileProps As New DSOFile.OleDocumentProperties
            FileProps.Open(file, True)
            Dim oSumProps As DSOFile.SummaryProperties
            oSumProps = FileProps.SummaryProperties
            If System.String.IsNullOrEmpty(oSumProps.Comments) = True Then
                Return "0"
            Else
                Return oSumProps.Comments.ToString
            End If

            oSumProps = Nothing
            FileProps.Close(False)
            FileProps = Nothing

    End Function

    I always get this error message.

    Unhandled Exception: System.IO.IOException: The process cannot access the file because it is being used by another process.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.Move(String sourceFileName, String destFileName)


    How can I get the OrigPath Function to release the lock it has on the source file?

    Wednesday, October 15, 2008 6:29 PM

Answers

  • The return statements you have in the code jump out of the function before your tidy up code;

    Try this instead

    Private Function OrigPath(ByVal file As String) As String  
        Dim FileProps As New DSOFile.OleDocumentProperties  
        FileProps.Open(file, True)  
        Dim oSumProps As DSOFile.SummaryProperties  
        oSumProps = FileProps.SummaryProperties  
     
        dim strReturn as String = Nothing 
        If System.String.IsNullOrEmpty(oSumProps.Comments) = True Then  
            strReturn = "0" 
        Else  
            strReturn = oSumProps.Comments.ToString  
        End If  
     
        oSumProps = Nothing 
        FileProps.Close(False)  
        FileProps = Nothing 
     
        return strReturn  
    End Function  
     
    • Marked as answer by SimonR47 Friday, October 17, 2008 1:25 PM
    Thursday, October 16, 2008 1:59 PM

All replies

  • The return statements you have in the code jump out of the function before your tidy up code;

    Try this instead

    Private Function OrigPath(ByVal file As String) As String  
        Dim FileProps As New DSOFile.OleDocumentProperties  
        FileProps.Open(file, True)  
        Dim oSumProps As DSOFile.SummaryProperties  
        oSumProps = FileProps.SummaryProperties  
     
        dim strReturn as String = Nothing 
        If System.String.IsNullOrEmpty(oSumProps.Comments) = True Then  
            strReturn = "0" 
        Else  
            strReturn = oSumProps.Comments.ToString  
        End If  
     
        oSumProps = Nothing 
        FileProps.Close(False)  
        FileProps = Nothing 
     
        return strReturn  
    End Function  
     
    • Marked as answer by SimonR47 Friday, October 17, 2008 1:25 PM
    Thursday, October 16, 2008 1:59 PM
  • Thank You for your help. This did the trick. 
    Friday, October 17, 2008 1:25 PM