This is a migrated thread and some comments may be shown as answers.

Rename file IOException

2 Answers 29 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 11 Dec 2013, 09:34 AM
Hi,

i want to rename my file. But i got a IOException. It says "The Process Cannot Access the File Because It Is Being Used by Another Process".

This is my Code:

<telerik:RadAsyncUpload ID="rauKachelUpload" runat="server" ChunkSize="0" Localization-Cancel="Löschen" Localization-Remove="Entfernen" Localization-Select="Auswählen"
  Culture="de-DE" Skin="MetroTouch" MaxFileInputsCount="1" OnFileUploaded="rauKachelUpload_FileUploaded">
</telerik:RadAsyncUpload>

Protected Sub rauKachelUpload_FileUploaded(sender As Object, e As FileUploadedEventArgs)
 
    Try
        Using fileStream As Stream = e.File.InputStream
            Using img As System.Drawing.Image = System.Drawing.Image.FromStream(fileStream)
                Dim h As Integer = img.Height
                Dim w As Integer = img.Width
                img.Dispose()
                Dim fileName As String = e.File.GetName()
                If w = MaxWidth And h = MaxHeight Then
                    rauKachelUpload.TargetFolder = "img/kachel_grafik"
 
                    Dim TimeStamp As String = DateDiff("s", "01/1/1970 12:00:00 AM", DateTime.Now)
                    fileName = "KI_" & TimeStamp & WelcheSparteUndGröße
 
                    KachelPfad = "~/img/kachel_grafik/" & fileName
                Else
                    KachelFalsch = True
                End If
                If KachelFalsch = False Then
 
                    e.File.SaveAs(fileName)
 
                    Page.ClientScript.RegisterClientScriptBlock([GetType](), "CloseScript", "redirectParentPage('VermittlerBearbeiten.aspx?ID=" & VermittlerID & "&KBFN=" & KachelPfad & "&NA=true" & "&fwg=" & WelcheSparteUndGröße & "&Ang1=" & hfAng1CHK.Value & "&Ang2=" & hfAng2CHK.Value & "&Ang3=" & hfAng3CHK.Value & "&Ang4=" & hfAng4CHK.Value & "&AngSrc1=" & hfKachelIMGSrcBaufi.Value & "&AngSrc2=" & hfKachelIMGSrcImmo.Value & "&AngSrc3=" & hfKachelIMGSrcPhoto.Value & "&AngSrc4=" & hfKachelIMGSrcAsse.Value & "');", True)
                Else
                    rnfIconNichtErzeugt.Visible = True
                End If
            End Using
        End Using
    Catch ex As Exception
        rnfIconNichtErzeugt.Visible = True
    End Try
 
End Sub

Without trying to rename my file, it works fine.

Thanks for reading.
Daniel

2 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 16 Dec 2013, 07:21 AM
I have still this Problem. No idea?

Thank you,
Daniel
0
Nencho
Telerik team
answered on 16 Dec 2013, 08:24 AM
Hello Daniel,

I would suggest you to manually close the fileStream, in order to make sure that it is not used by another process :

Protected Sub rauKachelUpload_FileUploaded(sender As Object, e As FileUploadedEventArgs)
 
        Try
            Using fileStream As Stream = e.File.InputStream
                Using img As System.Drawing.Image = System.Drawing.Image.FromStream(fileStream)
                    Dim h As Integer = img.Height
                    Dim w As Integer = img.Width
                    img.Dispose()
                    fileStream.Close()
                    Dim fileName As String = e.File.GetName()
                    If w = MaxWidth And h = MaxHeight Then
                        rauKachelUpload.TargetFolder = "img/kachel_grafik"
 
                        Dim TimeStamp As String = DateDiff("s", "01/1/1970 12:00:00 AM", DateTime.Now)
                        fileName = "KI_" & TimeStamp & WelcheSparteUndGröße
 
                        KachelPfad = "~/img/kachel_grafik/" & fileName
                    Else
                        KachelFalsch = True
                    End If
                    If KachelFalsch = False Then
 
                        e.File.SaveAs(fileName)
 
                        Page.ClientScript.RegisterClientScriptBlock([GetType](), "CloseScript", "redirectParentPage('VermittlerBearbeiten.aspx?ID=" & VermittlerID & "&KBFN=" & KachelPfad & "&NA=true" & "&fwg=" & WelcheSparteUndGröße & "&Ang1=" & hfAng1CHK.Value & "&Ang2=" & hfAng2CHK.Value & "&Ang3=" & hfAng3CHK.Value & "&Ang4=" & hfAng4CHK.Value & "&AngSrc1=" & hfKachelIMGSrcBaufi.Value & "&AngSrc2=" & hfKachelIMGSrcImmo.Value & "&AngSrc3=" & hfKachelIMGSrcPhoto.Value & "&AngSrc4=" & hfKachelIMGSrcAsse.Value & "');", True)
                    Else
                        rnfIconNichtErzeugt.Visible = True
                    End If
                End Using
            End Using
        Catch ex As Exception
            rnfIconNichtErzeugt.Visible = True
        End Try
 
    End Sub


Regards,
Nencho
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
AsyncUpload
Asked by
Daniel
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or