Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hai

i have problem in delete file from folder,if i try to it show error like "unable to delete,this file used by another process"

First i download image file from sql server as bytes and save as image in folder using filestream and dispose and close filestream,after i will set background image for panel.if i again come same function it show error.so pl guid me how to remove background image and delete file from folder,so only i can again download from sql and save as image in folder

The below code for download from sql as bytes and save as image in folder(SLD in folder name)
VB
 Dim imageData As Byte() = Nothing
        sqlCmd.Fill(dt)
        For Each row In dt.Rows
            imageData = DirectCast(row("SLD"), Byte())
            SLDFileName = row("SLDFileName")
        Next
        Dim newImage As Image = Nothing

        If Not imageData Is Nothing Then
            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                ms.Write(imageData, 0, imageData.Length)
                newImage = Image.FromStream(ms, True)
                Dim fs As FileStream = File.Create(AppPath + "SLD\" + SLDFileName)
                fs.Write(imageData, 0, imageData.Length)
                fs.Dispose()
                fs.Close()
            End Using
            ContainerSLD.BackgroundImage = Image.FromFile(AppPath + "SLD\" + SLDFileName) '-->here i set backgroungimage for panel

        End If


this below code for dispose and delete files from directory
VB
If (Not ContainerSLD.BackgroundImage Is Nothing) Then
          ContainerSLD.BackgroundImage.Dispose()
          ContainerSLD.Dispose()
          My.Computer.FileSystem.DeleteDirectory(System.Windows.Forms.Application.StartupPath + "\Diagram\SLD", FileIO.DeleteDirectoryOption.DeleteAllContents)'--> here i get Error,eventhoug i dispose image from panel.

      Else
      End If


pls reply asap


Regards
Aravind
Posted

1 solution

Try this,

Dim strDirectory As String = System.Windows.Forms.Application.StartupPath + "\Diagram\SLD"
For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory,FileIO.SearchOption.SearchTopLevelOnly,*.*)
   My.Computer.FileSystem.DeleteFile(foundFile, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently)
Next
 
Share this answer
 
v3
Comments
Aravindba 11-Oct-13 2:47am    
Hai Thank for ur reply,but i get error
For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory, "*.*")--Here i get error "Conversion from string "*.*" to type 'Integer' is not valid."
[no name] 11-Oct-13 3:19am    
FileIO.SearchOption.SearchTopLevelOnly
Please modify your code adding this parameter too and try,

For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory,FileIO.SearchOption.SearchTopLevelOnly,"*.*")
My.Computer.FileSystem.DeleteFile(foundFile, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently)
[no name] 11-Oct-13 3:19am    
hope this will work.
[no name] 11-Oct-13 11:52am    
have you undo the answer acepted??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900