如何在Windows应用程序中删除文件表单文件夹? [英] How to delete files form folder in windows application ?

查看:86
本文介绍了如何在Windows应用程序中删除文件表单文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai



i从文件夹删除文件有问题,如果我尝试显示错误,如无法删除,此文件被其他进程使用



首先我从sql server下载图像文件作为字节,并使用文件流保存为文件夹中的图像并处理并关闭文件流,之后我将为panel设置背景图像。如果我再次来同样的功能它显示error.so pl指导我如何从文件夹中删除背景图像和删除文件,所以只有我可以再次从sql下载并保存为文件夹中的图像



以下代码从sql下载为字节并保存为文件夹中的图像(SLD在文件夹名称中)

  Dim  imageData 作为 字节()= 无/ span> 
sqlCmd.Fill(dt)
对于 每个
dt.Rows
imageData = DirectCast (行( SLD),字节())
SLDFileName = row( SLDFileName
下一步
< span class =code-keyword> Dim newImage As Image = Nothing

如果 imageData Nothing 然后
使用 ms 作为 MemoryStream(imageData, 0 ,imageData.Length)
ms.Write(imag eData, 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()
结束 使用
ContainerSLD.BackgroundImage = Image.FromFile(AppPath + SLD \ + SLDFileName)' - > 这里我为面板设置了backgroungimage

结束 如果





以下代码用于处理和删除目录中的文件

< pre lang =vb> 如果 ContainerSLD.BackgroundImage Nothing 然后
ContainerSLD.BackgroundImage.Dispose()
ContainerSLD.Dispose()
My.Computer.FileSystem.DeleteDirectory(System.Windows.Forms.Application.StartupPath + \ Diagram \ SLD,FileIO.DeleteDirectoryOption.DeleteAllContents)' - > ;在这里我得到错误,eventhoug我从面板处理图像。

否则
结束 如果





请回复asap





问候

Aravind

解决方案

试试这个,



 Dim strDirectory As String = System.Windows.Forms.Application.StartupPath +\ Diagram \ SLD
For eachFile As String My.Computer.FileSystem.GetFiles(strDirectory,FileIO.SearchOption.SearchTopLevelOnly,*。*)
My.Computer.FileSystem.DeleteFile(foundFile,FileIO.UIOption。 AllDialogs,FileIO.RecycleOption.DeletePermanently)
Next


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)

 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

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

解决方案

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


这篇关于如何在Windows应用程序中删除文件表单文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆