IT说正在使用的文件我无法理解为什么
错误:
System.IO.IOException was unhandled Message=The process cannot access the file 'C:\Users\Bradley Smithies\AppData\Roaming\.minecraft\screenshots\2011-07-11_09.34.15.png' because it is being used by another process. Source=mscorlib StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at ScreenShotCraft.frmindex.btndeleteselected_Click(Object sender, EventArgs e) in D:\Coding\Visual Basic\ScreenshotCraft\frmindex.vb:line 107
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ScreenShotCraft.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:
CODE:
Imports System.Environment
Public Class frmindex
Dim output As String
Dim appdata As String = GetFolderPath(SpecialFolder.ApplicationData)
Dim path As String = appdata + "\.minecraft\screenshots"
Private Sub frmindex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not System.IO.Directory.Exists(appdata + "\.minecraft\screenshots") = True Then
' Creates a FolderBrowserDialog object
Dim fbdpath As New FolderBrowserDialog
'Dialog
With fbdpath
' Desktop is the path folder in the dialog.
' Select the C:\Windows directory on entry.
.SelectedPath = CurDir()
' Prompt the user with a custom message.
.Description = "Please Select Your Minecraft Screenshot Folder"
path = .SelectedPath
If .ShowDialog = DialogResult.OK Then
path = .SelectedPath
End If
End With
End If
Dim strBasePath As String = path
Dim dirInputFileInfo As IO.FileInfo
Dim dirInputInfo As New IO.DirectoryInfo(strBasePath)
Dim dirInputFileArray As IO.FileInfo() = dirInputInfo.GetFiles()
For Each dirInputFileInfo In dirInputFileArray
Me.clbfiles.Items.Add(dirInputFileInfo.Name)
Next
End Sub
Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
' Creates a FolderBrowserDialog object
Dim fbdoutput As New FolderBrowserDialog
'Dialog
With fbdoutput
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the C:\Windows directory on entry.
.SelectedPath = CurDir()
' Prompt the user with a custom message.
.Description = "Please select where you would like the pictures to be places"
output = .SelectedPath
If .ShowDialog = DialogResult.OK Then
output = .SelectedPath
txboutput.Text = output
End If
End With
End Sub
Private Sub clbfiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clbfiles.SelectedIndexChanged
If Not clbfiles.SelectedItem = "" Then
pcbpreview.Image = Image.FromFile(path + "\" + clbfiles.SelectedItem.ToString)
End If
End Sub
Private Sub btnselectall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnselectall.Click
For i As Integer = 0 To Me.clbfiles.Items.Count - 1
Me.clbfiles.SetItemChecked(i, True)
Next i
End Sub
Private Sub btnunselectall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnunselectall.Click
For i As Integer = 0 To Me.clbfiles.Items.Count - 1
Me.clbfiles.SetItemChecked(i, False)
Next i
End Sub
Private Sub btndeleteall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndeleteall.Click
For i As Integer = 0 To Me.clbfiles.Items.Count - 1
Me.clbfiles.SetItemChecked(i, True)
Next i
Dim CheckedItems(clbfiles.CheckedItems.Count - 1) As Object
clbfiles.CheckedItems.CopyTo(CheckedItems, 0)
For Each CheckedItem As Object In CheckedItems
Dim FileToDelete As String
'creates path to delete
FileToDelete = appdata + "\" + CheckedItem.ToString
If System.IO.File.Exists(FileToDelete) = True Then
'deletes
System.IO.File.Delete(FileToDelete)
MsgBox("File Deleted")
End If
'removes from list
clbfiles.Items.Remove(CheckedItem)
Next
End Sub
Private Sub btndeleteselected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndeleteselected.Click
Dim CheckedItems(clbfiles.CheckedItems.Count - 1) As Object
clbfiles.CheckedItems.CopyTo(CheckedItems, 0)
For Each CheckedItem As Object In CheckedItems
Dim FileToDelete As String
'creates path to delete
FileToDelete = path + "\" + CheckedItem.ToString
If System.IO.File.Exists(FileToDelete) = True Then
'deletes
System.IO.File.Delete(FileToDelete)<-----------ERROR HERE, say file in use
MsgBox("File Deleted")
End If
'removes from list
clbfiles.Items.Remove(CheckedItem)
Next
End Sub
Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
Dim CheckedItems(clbfiles.CheckedItems.Count - 1) As Object
clbfiles.CheckedItems.CopyTo(CheckedItems, 0)
For Each CheckedItem As Object In CheckedItems
Dim FileToDelete As String
'creates path to delete
FileToDelete = path + "\" + CheckedItem.ToString
If System.IO.File.Exists(FileToDelete) = True Then
If System.IO.File.Exists(output + "\" + CheckedItem.ToString) = False Then
'deletes
System.IO.File.Copy(FileToDelete, output + "\" + CheckedItem.ToString)
MsgBox("Files Copied")
End If
End If
'removes from list
Next
End Sub
Private Sub btnmove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmove.Click
Dim CheckedItems(clbfiles.CheckedItems.Count - 1) As Object
clbfiles.CheckedItems.CopyTo(CheckedItems, 0)
For Each CheckedItem As Object In CheckedItems
Dim FileToDelete As String
'creates path to delete
FileToDelete = path + "\" + CheckedItem.ToString
If System.IO.File.Exists(FileToDelete) = True Then
'deletes
System.IO.File.Move(FileToDelete, output + "\" + CheckedItem.ToString)
MsgBox("Files Moved")
End If
'removes from list
clbfiles.Items.Remove(CheckedItem)
Next
End Sub
End Class
答案 0 :(得分:2)
我认为您需要对图像预览做一些事情 - 您需要处理使用FromFile
加载的图像实例:
文件保持锁定状态,直到图像被丢弃。
你可能想做类似的事情:
Private Sub clbfiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clbfiles.SelectedIndexChanged
Dim prevImage = pcbpreview.Image
pcbreview.Image = Nothing
If Not prevImage Is Nothing then
prevImage.Dispose()
End If
If Not clbfiles.SelectedItem = "" Then
pcbpreview.Image = Image.FromFile(path + "\" + clbfiles.SelectedItem.ToString)
End If
End Sub
类似的抓取图像实例/设置预览null /在执行删除操作之前处理上一个图像。