我的应用程序锁定的文件 [英] files locked by my applicatoin

查看:143
本文介绍了我的应用程序锁定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时使用后

 File.Copy(sourse,dest)





 file.CopyTo(dest, true 



on一个大文件

当我想使用或删除目标文件时出现以下错误

进程无法访问文件' 文件路径',因为 正由另一个人使用进程。





如何强制我的应用程序发布文件的过程?

解决方案

您可以使用FileSystemWatcher并对OnChanged事件做出反应。尝试打开文件并捕获任何异常。继续努力,直到你成功。请注意,OnCreated事件会立即引发,所以不要使用它。



看看Steve Czetty在这篇文章 [ ^ ]示例。


即使某些其他进程访问同一文件,您也可能会收到此异常除文件复制操作外。

您可以使用以下方法检查文件是否锁定

c# - 有没有办法检查文件是否正在使用? - 堆栈溢出 [ ^ ]

你也可以用下面的单一方法(文件移动)替换复制删除操作

  if (!File.Exists(dest)){
File.Move(source,dest);
}


请参阅我的回答:另一个进程异常使用的文件 [ ^ ]。



-SA

sometimes after use

File.Copy(sourse,dest)


or

file.CopyTo(dest,true)


on a big file
when I want to use or delete the destination file get the following error

The process cannot access the file 'file path' because it is being used by another process.



how can i force the process of my application to release the file?

解决方案

You can use a FileSystemWatcher and react to the OnChanged events. Attempt to open the file and catch any exception. Keep trying until you are successful. Note the OnCreated event is raised immediately so don't use that.

Have a look at Steve Czetty's solution on this post[^] for an example.


You may get this exception even when some other process accessing same file other than file copy operation.
You can check file locked or not using below method
c# - Is there a way to check if a file is in use? - Stack Overflow[^]
also you can replace copy-delete operations with single method (file move) like below

if (!File.Exists(dest)) {
    File.Move(source,dest);
}


Please see my past answer: File used by another process exception[^].

—SA


这篇关于我的应用程序锁定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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