如何强制删除asp.net中Web Dev Server正在使用的文件 [英] How to I forcefully delete a file that is being used by Web Dev Server in asp.net

查看:229
本文介绍了如何强制删除asp.net中Web Dev Server正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试删除文件后对其执行某些操作。此文件存在于IIS中。尝试删除时,抛出异常。还尝试从资源管理器中手动删除,它显示一条消息由Web Dev Server使用。



如何强行删除该文件或将其替换为同一文件的新版本。



谢谢和问候,

Raghu

Hi,

I am trying to delete a file after doing certain operations on it. This file is present in IIS. When trying to delete, throwing the exception. Also tried to delete manually from Explorer, it shows a message, "Being used by Web Dev Server".

How do I delete that file forcefully OR Replace that with the new version of the same file.

Thanks & Regards,
Raghu

推荐答案

不要强迫它,因为它更像是结束任务。正在使用的文件意味着进程Web Dev Server依赖于此文件,因为某些组件可能正在使用该文件中提供的数据。如果您强制删除该文件,该过程将被终止,您的网站将面临停机。



为了解决这个问题,安全地关闭正在使用它的进程。您可以使用其他界面或对象,在您需要时为您执行此操作。我的安全意思是,您的进程保存状态,将数据保存到磁盘,以服务器不会停止或您的网站不会面临停机的方式结束。



解决此类问题的一个好方法是释放您正在使用的资源。您可能正在使用 Stream FileStream 具体)来提供给该文件你的过程,你忘了发布流。永远记得在终止流程之前释放资源,因为它会导致 内存泄漏 [ ^ ]。您的这种情况是内存泄漏或使用该文件的进程的结果。如果案例是正在使用该文件的进程,则可以关闭它,并且可以释放资源,在更糟糕的情况下,例如当进程终止但资源未被释放时。您的文件将保持打开状态,您必须重新启动计算机。



在大多数情况下, 使用 [ ^ ]语句可帮助您自动释放资源。我还想在您的应用程序中推荐使用它。



了解在C#中使用'使用'语句 [ ^ ]



只有这样,您才能以良好的方式删除文件。当然,您现在可以手动删除文件,让流程崩溃,让网站面临停机,让您的用户对您的503消息感到恼火。但是,这不是一个很好的方法,是吗?只要正在使用文件,即使替换文件也不会完成。因此,您的进程必须停止,等待文件刷新,然后再次启动。
Do not force it, because it more like to "End task". The file being used means, that the process "Web Dev Server" is relying on this file as some of the components may be using the data provided in that file. If you force delete that file, the process would be terminated and your website would face a downtime.

To overcome this, "safely" close the process that is using it. You can use another interface or an object, that does that for you when you want to. What I mean by safely is, that your process saves the state, saves the data to the disk, ends itself in a manner that your server doesn't get halted or your website doesn't face a downtime.

A good solution to such problems is to release the resources that you are using. You may be using a Stream (FileStream specifically) to that file which is provided to your process, and you forgot to release the stream. Always remember to release the resources before terminating the processes as it leads to memory leak[^]. This case of yours, is a result of memory leak or a process using the file. If the case is "process currently using the file" then it can be closed, and resource can be released, in worse cases such as when the process is terminated but the resource wasn't released. Your file is going to remain open, and you would have to restart the machine.

In most of these cases, using[^] statement helps you release the resources automatically. I would also like to recommend using this, in your application.

Understanding the 'using' statement in C#[^]

Only then, you will be able to delete the files in a "good manner". Now of course you can just manually delete the file, let the process crash, make the website face a downtime and get your users annoyed at your 503 messages. But, that is not a good way of doing it, is it? Even replacing the file won't be done as long as the file is being used. So your process has to stop, wait until the file is refreshed, then start once again.


这篇关于如何强制删除asp.net中Web Dev Server正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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