“进程无法访问该文件,因为它正在被另一个进程使用”

我需要将一个CSVfile upload到Azure服务器上的ASP.NET应用程序。 虽然它在我的本地机器上正常工作,但是在上传到服务器时会引发以下错误:

“进程无法访问文件”C:\ inetpub \ wwwroot \ ImportFiles \ 9_11.csv“,因为它正在被另一个进程使用”

我的代码:

string fileName = DateTime.Now.ToString().Replace(':', '_').Replace('/', '_').Replace(' ', '_') + Convert.ToString((new Random()).Next(0, 999) * (new Random()).Next(0, 999)); string path = Server.MapPath("ImportFiles") + "\\" + fileName + "" + FileImport.FileName.Substring(FileImport.FileName.IndexOf('.')); FileImport.SaveAs(path); string pathforSeconStream = path; try { Response.Write("<script> alert('In Try Block');</script>"); bool flag = true; int visiblemessageCount = 0; int rollNo = 1; StreamReader ColLine = new StreamReader(path); string ColInputline = ColLine.ReadLine(); String[] ColsInput = ColInputline.Split(','); ColLine.Close(); ColLine.Dispose(); string preFix = "", RollNumber = ""; StreamReader sr = new StreamReader(pathforSeconStream); } catch(Exception ex) { } 

生成唯一文件名的代码是错误的。 使用Path.GetTempFileName 。

PS从不吃东西。 请删除catch (Exception ex) {} ;

调整

而不是FileImport.Save(...)只是将请求保存在MemoryStream中 ,然后处理它。