如何关闭文件流 [英] How to close file stream

查看:240
本文介绍了如何关闭文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传了如下文件。



Hi , I upload a file like below.

File_Upload.PostedFile.SaveAs(Server.MapPath("L:/.../a.jpg"));





我想用以下代码删除我的文件。





And i would like to delete my file with the below code.

System.IO.File.Delete("L:/.../a.jpg");





我遇到了这个错误:



进程无法访问该文件'L:\ chikardarin Website\WebApplication2\image_Logo\16_honda-cars-logo-emblem.jpg'因为它被另一个进程使用。



请帮助我如何关闭此流以执行我的删除操作???



编辑:



我的确切上传代码是:





I faced to this error :

The process cannot access the file 'L:\chikardarin Website\WebApplication2\image_Logo\16_honda-cars-logo-emblem.jpg' because it is being used by another process.

Please help me how can i close this stream to perform my delete action ???

Edit :

my exact upload code is :

string fileName = Path.GetFileName(File_Upload.PostedFile.FileName);
string imgid = "car";
string path = (ConfigurationManager.AppSettings["virtual_path"]).ToLower();
path = path + ImgId + "_" + fileName;
File_Upload.PostedFile.SaveAs(Server.MapPath(path));





现在哪个是我的资源?我上传文件的方式是真的吗?



now which is my resource ? is my way to upload file true ?

推荐答案

请看我过去对这个问题的回答:如何压缩错误'它已被vb.net中的另一个进程'使用 [ ^ ]。



-SA
Please see my past answer on this problem: how to compress the error 'it is already used by another process' in vb.net[^].

—SA


我无法告诉您哪些代码可能正在使用资源文件。但是,您可以使用此方法关闭资源。



I cannot tell you which code might be consuming the resource file. But, however, you can use this method to close the resource.

resource.Close();





大多数情况下,溪流会显示这种行为,一旦你完成工作就可以关闭它们。



推荐



我想建议你使用使用使用资源时阻止。这确保了正确的处置或资源,或者从技术上讲,确保了IDisposable对象。有关更多信息,请阅读本文档。 http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]



Mostly, streams show this kind of behaviour and you can close them once you're done working.

Recommendation

I would like to recommend you that you should use the using block while working with the resource. This ensures the correct disposal or resources, or to be technically speaking, IDisposable objects. For more, please read this document. http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^]

这篇关于如何关闭文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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