错误 - 文件正在使用中 [英] error - file is in use

查看:60
本文介绍了错误 - 文件正在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!



我正在拍摄用户的照片,然后保存它们。每次用户点击他的照片时,它都会保存在Images文件夹中。现在,用户可以拍摄他喜欢的尽可能多的照片,但是如果为该用户拍摄,则特定用户的每张照片都会替换前一张照片,因此我总是只有1张照片供1位用户使用。现在有时它说文件正在使用中。我该怎么办 ??



这里是代码。



hello!!

i am taking photographs of the user and then saving them . every time user clicks his photograph is taken and then it gets saved inside Images folder. now the user can take as many photographs as he likes but each photograph for a particular user replaces the previous one if taken for that user so i always have only 1 photograph for 1 user. now sometimes it says file is in use. what should i do ??

here is the code.

void CreatePhoto()
    {
        try
        {
            filename = getPassNo() + "-"+ DateTime.Now.Year.ToString();

            if( File.Exists(Server.MapPath("Images/" + filename)))
            {
                
                File.Delete(Server.MapPath("Images/" + filename));
  
            }
            string strPhoto = Request.Form["imageData"]; //Get the image from flash file
            byte[] photo = Convert.FromBase64String(strPhoto);
            FileStream fs = new FileStream(Server.MapPath("Images/" + filename), FileMode.OpenOrCreate, FileAccess.Write);
            
            BinaryWriter br = new BinaryWriter(fs);
            
            br.Write(photo);
           
            br.Flush();
          
            br.Close();
           
            fs.Close();
            string x = "Update Image Set Taken =1";
            Dal.ExnonQry(x);
         

        }
        catch (Exception Ex)
        {

        }
        finally
        {
           
            Response.Redirect("Default.aspx?FN=" + filename + "");
        }
    }

推荐答案

File.Delete,File.Copy File.Move方法附带此问题:在操作系统被告知要做什么之后,他们立即返回。但操作系统可能需要更多时间在磁盘上执行操作。因此,您可以在编写下一个文件之前检查文件是否仍然存在(并在必要时等待)。

顺便说一句,我更喜欢将新照片保存到临时文件中,然后将它移动到最终位置:如果在服务器上创建照片时出现问题,您仍然可以将旧图像保留在那里。
This problem comes with the File.Delete, File.Copy File.Move methods: they return immediately, just after the OS has been told what to do. But the OS may require a little more time to perform the action on the disk. Hence you could check if the file still exists (and wait if necessary) before you write the next file.
By the way, I'd prefer to save the new photo to a temporary file, and then move it to its final location: if osmething goes wrong with creating the photo on the server, you could still keep the old image there.


这篇关于错误 - 文件正在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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