FileStream can't access the file because it's being used by another process

◇◆丶佛笑我妖孽 提交于 2019-12-20 02:11:33

问题


I'm new at web app in ASP.NET and I came across this problem.

I have a page whe there is a Button to download a template.xls that is previously stored at a SharePoint page. I have the download method and it's working just fine. The template in question is being saved where it should be, on a folder where my web app is located, on my local IIS. The problem is to open this file to the end user. I need a popup to be displayed to the user, so he can open/save this template.xls I'm using the following :

//path on my local IIS where the file is located after the download
string _strFolderApp = "~/Arq/";
string _strFullFolderApp = _strFolderApp + _strFileName;
string apPath = System.Web.Hosting.HostingEnvironment.MapPath(_strFullFolderApp);

FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
// Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(_contentFile, 0, _contentFile.Length);

//opens the file
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
Response.ContentType = "";
Response.TransmitFile(apPath);
Response.Flush();
File.Delete(apPath);
Response.End();
# endregion

// close file stream
_FileStream.Close();

I searched online and all answers end up in using FileShare.ReadWrite so both process would work properly. But it's not working for me, because when the code reaches Response.TransmitFile(apPath); I get an exception and popup doesn't show. Exception :

The process cannot access the file 'c:\inetpub\wwwroot\MyWebApp\File\TemplateSharePoint.xlsx' because it is being used by another process.

Please any help would be appreciated :)

EDIT Code update

if (_flgSPSDownload)
        {
            System.IO.FileStream _FileStream = new System.IO.FileStream(apPath,System.IO.FileMode.Create, System.IO.FileAccess.Write);
            _FileStream.Write(_contentFile, 0, _contentFile.Length);
            _FileStream.Close();


            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
            //Set the appropriate ContentType.
            Response.ContentType = "Application/x-msexcel";
            Response.TransmitFile(apPath);
            Response.Flush();
            //Write the file directly to the HTTP content output stream.
            Response.WriteFile(apPath);
            //File.Delete(apPath);

            //Process.Start(_strDestinationPath + "//" + _strFileName);

回答1:


From what I can tell, you're opening the file-stream, and then trying to open it again, before you close it.

Initial opening of the file:

FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

and

Response.TransmitFile(apPath); 

seems to be trying to open the file again.

I would suggest calling

_FileStream.Close();

before calling TransmitFile.




回答2:


i have solved this problem by another way

i open Window Task Manager and find that my dll file of my project is running

multiples ( i mean copy of it exists) i remove them and than my problem is solved



来源:https://stackoverflow.com/questions/13297466/filestream-cant-access-the-file-because-its-being-used-by-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!