【问题标题】:"Process cannot access the file because it is being used by another process"“进程无法访问该文件,因为它正被另一个进程使用”
【发布时间】:2015-12-07 21:51:08
【问题描述】:

我需要将 CSV 文件上传到 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)
{
}   

【问题讨论】:

    标签: c# asp.net excel streamreader data-import


    【解决方案1】:

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

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

    修订

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

    【讨论】:

    • 谢谢,但我需要将该文件保存在两个流对象中以供进一步处理。首先我将流保存在对象“ColLine”中,然后将流保存在“sr”对象中..但它通过错误消息虽然它在我的开发机器上完美运行。
    • @Shahab 您显示的代码在 try 块中没有任何作用。如果还有更多未显示的代码,您可以随时重置MemoryStream 的当前位置。
    • 有很多我无法通过的代码,这使得我的问题使用较少,没有人愿意阅读大量代码,因为有一个查询在匹配两个数组列表值后执行。
    猜你喜欢
    • 2010-12-10
    相关资源
    最近更新 更多