进程无法访问该文件,因为它正由另一个进程使用! [英] Process cannot access the file because it is being used by another process!

查看:186
本文介绍了进程无法访问该文件,因为它正由另一个进程使用!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法想象这一直在研究。更改代码结构,Everthing我可以想到让这个工作,但它一直给我错误这里是我得到的错误我很确定在代码中的某个地方它保持文件但我不知道哪个两个头更好一个我猜。

I cannot figur this out ive been researching. Changeing code structure, Everthing I could think of to get this to work but it keep giving me errors here is the error I get Im pretty sure somewhere in the code its holding onto the file but I dont know where Two heads are better one I guess.

System.IO.IOException: The process cannot access the file because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalMove(String sourceDirName, String destDirName, Boolean checkHost)
at System.IO.Directory.Move(String sourceDirName, String destDirName)
at TLU_LAUNCHER.Form1.PictureBox3_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)







Imports System
Imports System.IO
Imports System.IO.Directory
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System.IO.DirectoryInfo
Imports System.IO.Path
<assembly: xmlns:assembly="#unknown"> 
Public Class Form1
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.Close()
    End Sub
    Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.Image = My.Resources.Exit_2
    End Sub
    Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.Image = My.Resources._Exit
    End Sub
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Process.Start("FILE NAME")
        MsgBox("Downloading Arma 3 Mods")
    End Sub
    Private Sub PictureBox2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.MouseEnter
        PictureBox2.Image = My.Resources.Download_2
    End Sub
    Private Sub PictureBox2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.MouseLeave
        PictureBox2.Image = My.Resources.Download
    End Sub
    <fileiopermissionattribute(securityaction.permitonly,>    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
    PathDiscovery:="C:\All Users\Desktop"), _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
    Append:="C:\Documents and Settings\All Users"), _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
        Write:="C:\Documents and Settings\All Users"), _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
        ViewAndModify:="C:\Documents and Settings\All Users")> _
    Public Shared Sub PermitOnlyMethod()
    End Sub
    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        IsArray("\TLU")
        Dim path2 As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
        IsArray("\Program Files (x86)\Steam\steamapps\common\Arma 3")
        System.Threading.Thread.Sleep(3000)
        Directory.Move(path, (path2))
        MsgBox("File Move")
    End Sub
    Private Sub PictureBox3_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseEnter
        PictureBox3.Image = My.Resources.Move_File_2
    End Sub
    Private Sub PictureBox3_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseLeave
        PictureBox3.Image = My.Resources.Move_File
    End Sub
End Class</assembly:>

推荐答案

这是PictureBo众所周知的问题x(实际上是由Bitmap构造函数)。请参阅此SO答案以获得解决方法:



http://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath/8701748#8701748 [ ^ ]



/ ravi
This is a well known problem with PictureBox (actually by the Bitmap constructor). See this SO answer for a workaround:

http://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath/8701748#8701748[^]

/ravi


Allways会检查您是否可以在打开文件之前打开文件。



来自这里



Allways check if you are allowed to open a file before you do open it.

Example from here

public bool CheckIfFileIsBeingUsed(string fileName){

     try{

         File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);

      }

      catch (Exception exp){

           return true;

      }

      return false;

}


这篇关于进程无法访问该文件,因为它正由另一个进程使用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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