使用File.Move重命名文件给IOException

nei*_*man 3 .net c#

我正在编写一个简单的应用程序,它将使用当前文件名之前的日期/时间重命名JPEG.这样我就可以将我拍摄的所有照片与我的伴侣照片(不同的相机制作和文件名)结合起来.

以下代码是发生故障的地方:

private void RenameFile(String oldFilename, String newFilename)
{
    if (File.Exists(oldFilename)
    {
        File.Move(oldFilename, newFilename);
    }
}
Run Code Online (Sandbox Code Playgroud)

示例值:oldFilename ="E:\ 001.jpg"| newFilename ="E:\ 2009-08-07 06h05 - 001.jpg"

我得到的例外是:

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()
Run Code Online (Sandbox Code Playgroud)

如果我使用File.Copy而不是它工作,但我有两个文件,不能删除原文并使用File.Delete我得到相同(或类似)的异常.

在一些测试中,如果我将它重命名为E:\ a001.jpg那么它似乎工作?文件名有效,因为我可以使用Windows资源管理器重命名它.:S

有任何想法吗?一些重命名工作的事实似乎说它不是锁定问题?

干杯尼尔

Jam*_*mes 9

请参阅MSDN上的File.Move.

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


nei*_*man 5

感谢所有发布的人.我设法使用以下方法

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

    if (file.Exists)
    {
        File.Move(oldFilename, newFilename);
    }
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我原来没有用,但上面做了.

真正的问题是我正在调试仍然导致抛出IOException的代码.如果我运行构建的应用程序,它工作正常!

再次感谢!当它让我重命名为某些名字时,我简直无法相信它......一定是看到了东西!

尼尔