使用c#强制删除日志文件 [英] Force Delete A Log File Using c#

查看:363
本文介绍了使用c#强制删除日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者。我尝试删除其他程序使用的Log.txt文件。但它显示错误



System.IO.IOException:进程无法访问文件'D:\TEST\Log.txt'因为它正被另一个进程使用。



MyCode在C#



I am beginner in C#. I try to Delete Log.txt files used by another program. but it shows error

System.IO.IOException: The process cannot access the file 'D:\TEST\Log.txt' because it is being used by another process.

MyCode is in C#

foreach (string file in Directory.GetFiles(path)){
           File.Delete(file);}



请帮我解决如何强制删除文件在c#中使用。或者如何在特定资源中释放所有进程(例如:D:\TEST \Log.txt)。在此先感谢



此文件夹路径由另一个服务创建的100多个日志文件。当某些文件显示错误时删除所有文件。


Please help me how to force delete files using in c#. or how to release all process in specific resource(Example:D:\TEST\Log.txt). Thanks in Advance

this folder path more than 100 log files created by another services. while deleting all files when some files shows error.

推荐答案

无法强行删除文件。即使你这样做,也可能导致文件锁定打开的进程崩溃。



您必须等待文件打开的进程关闭它或者退出写入文件的进程。
There is no way to force delete a file. Even if you did, you could cause the process that has the file locked open to crash.

You have to either wait for the process that has the file open to close it or quit the process that writing to the file.


在写入日志文件之前不能删除日志文件。

确保写入此日志文件已经停止。一旦锁定文件不再存在,您可以通过代码删除该文件。
You cannot delete a log file till it is being written into.
Make sure writing to this log file has stopped. Once the lock on the file ceases to exist, you can delete the file through code.


这篇关于使用c#强制删除日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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