Click here to Skip to main content
14,971,029 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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)


VB
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:>
Posted
Updated 14-Jan-15 4:49am
v2

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
   
Comments
Zach Suggs 14-Jan-15 11:26am
   
Im a bit confused Im extremely new to this code and the link you gave me his link is not working so I couldn't get further information how am i suppose to make that line of code move a directory?
Ravi Bhavnani 14-Jan-15 12:05pm
   
My point is, once an image file has been shown in a PictureBox, the file is locked until the PictureBox is garbage collected (e.g. when your app exits). If you use the suggestion in the link (the link works fine) your file will not be locked. This will allow it to be moved/deleted/renamed.

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

Example from here

C#
public bool CheckIfFileIsBeingUsed(string fileName){

     try{

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

      }

      catch (Exception exp){

           return true;

      }

      return false;

}
   
Comments
Zach Suggs 14-Jan-15 11:27am
   
I have already Tried this and it doesnt help I put it right before the Directory.move(path, (path2)). Thanks for your feedback!
Herman<T>.Instance 14-Jan-15 13:44pm
   
I what construction?
if (CheckIfFileIsBeingUsed("file name"))
{
Move
}
or in
if (!CheckIfFileIsBeingUsed("file name"))
{
Move
}

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