C#:使用File.Move给IOException异常重命名文件 [英] C#: Rename file using File.Move give IOException

查看:817
本文介绍了C#:使用File.Move给IOException异常重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个简单的应用程序,将JPEG文件重命名与他们当前的文件名前拍摄的日期/时间。这是这样,我可以把所有我带着我的那些伙伴的照片(不同的摄像机制造和文件名)。

I am writing a simple application that will rename JPEGs with the date/time they were taken before the current filename. This is so that I can combine all the photos that I took with those of my partner (different camera makes and filenames).

下面这段code是在故障发生:

The following piece of code is where the failure occurs:

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

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

Example values: 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()

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

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

在一些测试,如果我重命名为E:\ a001.jpg然后它似乎工作?文件名是有效的,因为我可以使用Windows资源管理器重新命名。 :•

During some tests, if I rename it to E:\a001.jpg then it seems to work?? The filename is valid as I can rename it using Windows Explorer. :S

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

Any ideas? The fact that some renames work seems to say it isn't a locking issue?

干杯尼尔

推荐答案

感谢大家谁张贴。我设法得到它与下面的方法

Thanks to everyone who posted. I managed to get it working with the following method

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

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

奇怪的是,我原来没有工作,但上面没有。

Strangely, my original didn't work but the above did.

真正的问题是,我是调试code这还是引起了IOException异常被抛出。如果我运行内置的应用程序,它工作得很好!

The real issue was that I was debugging the code which still causes the IOException to be thrown. If I run the built application, it works fine!

再次感谢!我简直不敢相信它时,它会让我重新命名为某些名字......一定是看花眼了!

Thanks Again! I just couldn't believe it when it would let me rename to certain names... must have been seeing things!

尼尔

这篇关于C#:使用File.Move给IOException异常重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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