Rename the file using File.Move, provide an IOException

I am writing a simple application that will rename JPEGs with the date / time they were made before the current file name. This is so that I can combine all the photos that I took with the photos of my partner (different cameras and file names).

The following code snippet where the crash occurs:

private void RenameFile(String oldFilename, String newFilename)
{
    if (File.Exists(oldFilename)
    {
        File.Move(oldFilename, newFilename);
    }
}

Example values: oldFilename = "E: \ 001.jpg" | newFilename = "E: \ 2009-08-07 06h05 - 001.jpg"

The exception that I get is:

System.IO.IOException was unhandled
  Message=The process cannot access the file because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.Move(String sourceFileName, String destFileName)
       at RenamePhotos.Form1.btnRenamePhotos_Click(Object sender, EventArgs e) in C:\Users\Neil Deadman\Desktop\RenamePhotos\RenamePhotos\Form1.cs: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 System.Windows.Forms.Application.Run(Form mainForm)
       at RenamePhotos.Program.Main() in C:\Users\Neil Deadman\Desktop\RenamePhotos\RenamePhotos\Program.cs:line 18
       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()

If I use File.Copy instead, then it works, but I have two files and I can’t delete the original and use File.Delete. I get the same (or similar) exception.

, E:\a001.jpg, ? , Windows.: S

? , , , , ?

+3
10

, . .

private void RenameFile(String oldFilename, String newFilename)
{
    FileInfo file = new FileInfo(oldFilename);

    if (file.Exists)
    {
        File.Move(oldFilename, newFilename);
    }
}

, , .

, , IOException. , !

! , ... , !

Neil

+6

. File.Move MSDN.

You cannot use the Move method to overwrite an existing file.

+9

, , , , , , , . File.Move(oldFilename, newFilename);, ?

+4
private  bool IsFileLocked(string filename)
{
    FileInfo file = new FileInfo(filename);
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}
public void MoveFile(string from, string to)
{
    try
    {
        FileInfo file = new FileInfo(from);
        // check if the file exists

        if (file.Exists)
        {
            // check if the file is not locked
            if (IsFileLocked(from) == false)
            {
                // move the file
                File.Move(from, to);
            }
        }
    }
    catch (Exception e)
    {
        ;
    }
}
+3

, , , . , . , , .

0

:

0

, - , .. / ?

, .

FileMon, , .

0

, , - , , . Process Explorer , , .

0

, File.Move( " , " ), , , , . , ; , , , . , " ".

0

.

-2

Source: https://habr.com/ru/post/1725068/


All Articles