本文介绍了上传后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传与FileUpload控件的文件。当文件上传,我从中提取信息,然后我想删除它。

我设法把它上传,保存它,并从中获得信息,但是当我尝试删除它,我得到follwing异常
该进程无法访问该文件。D:\\ IIS **的忽略的*** \\ V75个人ny.csv',因为它正被另一个进程使用

 字符串FN = Path.GetFileName(fu.PostedFile.FileName);
        串SaveLocation =使用Server.Mappath(UploadedCSVFiles)+\\\\+ FN;
        FileInfo的网络连接=新的FileInfo(SaveLocation);            fu.PostedFile.SaveAs(SaveLocation);
            fu.PostedFile.InputStream.Dispose();
            DataTable的DT = AMethodThatUsesFile(SaveLocation);
            fi.Delete();


解决方案

试试这个code删除文件。

  System.IO.File.Delete(SaveLocation);

您指定的方法AMethodThatUsesFile(SaveLocation);.如果它使用类似的StreamReader的类读取文件时,请关闭使用StreamReader.Close()读卡器;试图删除之前的方法

I am trying to upload a file with the FileUpload control. When file is uploaded, I extract information from it and then i want to delete it.

I manage to upload it, save it and get the info from it, but when i try to delete it i get the follwing exception"The process cannot access the file 'D:\IIS**OMITTED***\V75 personal ny.csv' because it is being used by another process.

 string fn = Path.GetFileName(fu.PostedFile.FileName);
        string SaveLocation = Server.MapPath("UploadedCSVFiles") + "\\" + fn;
        FileInfo fi = new FileInfo(SaveLocation);

            fu.PostedFile.SaveAs(SaveLocation);
            fu.PostedFile.InputStream.Dispose();
            DataTable dt = AMethodThatUsesFile(SaveLocation);
            fi.Delete();
解决方案

Try this code to delete file.

            System.IO.File.Delete(SaveLocation );

You specified a method AMethodThatUsesFile(SaveLocation);. If it uses any classes like StreamReader to read file, please close the reader using StreamReader.Close(); method before trying to delete

这篇关于上传后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:27