2010-07-21 3 views
0

VB에서 작성된 ASP.NET 인트라넷 응용 프로그램이 있습니다. 사용자로부터 파일을 가져온 다음 몇 가지 다른 경우에 따라 파일의 사본을 몇 개 만들고 원본을 이동할 수도 있습니다.ASP.NET 파일 업로드/릴리스 대기는 어떻게합니까?

Exception Details: System.IO.IOException: The process cannot access the file 
'\\some\dir\D09_03_5_180_0.000-6.788.png' because it is being used by 
another process. 

My.Computer.FileSystem.CopyFile에 의해 발생합니다 :이 오류가 어디

는 불행하게도 나는 경우 건너했습니다. 그리고 그것은 다른 프로세스에서 사용 중입니다. 사용자가 저장/다운로드 할 수 있거나 다른 스레드 (?)가 복사하는 동안 복사하려고 할 수도 있습니다. 실제로 알고 싶지 않은 내용은 다음과 같습니다.

파일을 더 이상 사용하지 않을 때까지 VB에서 파일을 복사 (이동) 할 때까지 기다리는 방법이 있습니까?

감사합니다.

답변

0

파일이 사용 중인지와 필요한 작업을 테스트합니다.

공공 하위 WriteLogFile (문자열로 ByVal의 pText, 문자열로 ByVal의 psPath, 문자열로 ByVal의 psName)

Dim strFullFileName As String 
    Dim Writer As System.IO.StreamWriter 
    Dim Fs As System.IO.FileStream 

    Try 
    Dim DirectoryHandler As New System.IO.DirectoryInfo(psPath) 
    strFullFileName = psPath & "\" & psName & Date.Today.Month.ToString & "-" & Date.Today.Day.ToString & "-" & Date.Today.Year.ToString & ".txt" 
    If Not DirectoryHandler.Exists() Then 
     Try 
      Monitor.Enter(fsLocker) 
      DirectoryHandler.Create() 
     Finally 
      Monitor.Exit(fsLocker) 
     End Try 
    End If 

    Try 
      If CheckIfFileIsInUse(strFullFileName) = True Then 
      Thread.Sleep(500) ' wait for .5 second 
      WriteLogFile(pText, psPath, psName) 
      If Not Fs Is Nothing Then Fs.Close() 
      If Not Writer Is Nothing Then Writer.Close() 
      Exit Sub 
     End If 
     Monitor.Enter(fsLocker) 
     Fs = New System.IO.FileStream(strFullFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Write) 
     Writer = New System.IO.StreamWriter(Fs) 
     Writer.WriteLine(Date.Now.ToString & vbTab & "ProcessID: " & Process.GetCurrentProcess.Id.ToString() & vbTab & pText) 
     Writer.Close() 
     Fs.Close() 
    Finally 
     Monitor.Exit(fsLocker) 
    End Try 

    Catch ex As Exception 

    Dim evtEMailLog As System.Diagnostics.EventLog = New System.Diagnostics.EventLog() 
    evtEMailLog.Source = Process.GetCurrentProcess.ProcessName.ToString() 
    evtEMailLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error) 

    Finally 
    If Not Fs Is Nothing Then Fs.Close() 
    If Not Writer Is Nothing Then Writer.Close() 
    End Try 

최종 하위 (문자열로 ByVal의 sFile) 공공 기능 CheckIfFileIsInUse 부울 으로 System.IO합니다. 그리고 짧은 = FreeFile() 제공 FileOpen (F는, sFile이 OpenMode.Append, OpenAccess.Write, OpenShare.Shared를) FileClose (F) 캐치 반환 진정한 끝 시도로 희미한 F를 시도 File.Exists (sFile) 최종 사용자 최종 기능

0

음 ... 직접 입력하지 마십시오. 멋진 UI를 만들고 싶다면 작은 시간대 (몇 초)로 파일 복사를 다시 시도하고 있습니다.

Ajax를 통해 복사 프로세스가 잘 진행되었는지 확인하십시오.

0

글쎄, 그 대기이 경우 작동하지 않을 밝혀 :

같은 위치에 한 위치에서 파일을 복사 할 수 없습니다 또는 오류가 (분명히)가 발생합니다 파일을 복사하려고합니다. 파일 복사를 시도하는 대신 VB는 실제로 파일을 복사하려고 시도하며 복사 작업이 복사중인 파일 (예 : overwrite:=True 이상)로 복사하려고하기 때문에 실패합니다.

웁스!