我正在编写一个简单的应用程序,将重命名JPEG的日期/时间,他们是在当前文件名之前。这是为了将我拍摄的所有照片与我的伴侣的照片(不同的相机制造商和文件名)结合在一起。
下面这段代码是发生故障的地方:
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”
我得到的例外是:
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时,我得到相同(或类似)的异常。
在某些测试中,如果我将其重命名为E:\a001.jpg,则它似乎可以工作??文件名有效,因为我可以使用Windows资源管理器对其进行重命名。:S
有什么想法吗?一些重命名工作的事实似乎表明这不是一个锁定问题?
干杯尼尔
发布于 2009-12-09 01:16:48
感谢每一位发帖的人。我用下面的方法让它工作起来
private void RenameFile(String oldFilename, String newFilename)
{
FileInfo file = new FileInfo(oldFilename);
if (file.Exists)
{
File.Move(oldFilename, newFilename);
}
}奇怪的是,我的原件不起作用,但上面的起作用了。
真正的问题是,我正在调试代码,这仍然会导致抛出IOException。如果我运行构建的应用程序,它工作得很好!
再次感谢!我只是不敢相信它会让我重命名为某些名字...一定是看见什么了!
尼尔
发布于 2009-12-09 00:42:21
请参阅MSDN上的File.Move。
特别是You cannot use the Move method to overwrite an existing file.
发布于 2009-12-09 00:40:14
事实上,一些重命名工作似乎表明这不是一个外部锁定问题,但可能是在您的程序中。如果你在File.Move(oldFilename, newFilename);上设置了一个断点,你还能在浏览器中重命名它吗?
https://stackoverflow.com/questions/1868173
复制相似问题