获取该文件正在服务器上使用但在本地系统上正常工作的错误

时间:2015-01-30 15:34:31

标签: c# asp.net

我很困惑它是服务器问题还是编码问题。我强烈猜测它可能是服务器问题,因为它在我的本地系统上工作正常。

我已将Asp.net 1.1网站升级到4.0。在应用程序中,文件由用户在表单上输入的值创建。该文件保存在应用程序的附件文件夹中。创建文件后,将电子邮件发送给管理员,并将该文件作为附件附加。

在我的本地系统上,电子邮件发送得很好。由于应用程序是在1.1中构建的,CDO正用于发送电子邮件。当我在服务器上发布应用程序时,附件失败并显示以下错误

The process cannot access the file 'E:\HostingSpaces\testuser\testapplication.mydomain.com\wwwroot\eTest\Attachment\4orsysil3dulr1iv1thvpade\ef_Comp.exp' because it is being used by another process.

我已经给了附件文件夹的读,写,删除权限。如果代码中存在问题,那么它也应该影响本地系统上的应用程序。我已经检查过所有StreamWriter在任何地方都被关闭了。

如果这是服务器错误,那可能是什么原因?

编辑: 代码是很久以前写的很老,它工作得很好。我所做的是更改了电子邮件发送代码并指定了SSL和用于发送SMS的新端口。除此之外它之前没有给出错误。

以下是用户在提交表单时生成文件的功能。每个表单都会生成多个文件。

   private bool GenerateFile()
        {
        string strSupportDocFile=string.Empty;
        string strBespokeFile=string.Empty;

        EFormDetails objEFormDetails=new EFormDetails();
        DataRow drEForm=objEFormDetails.ResultRow;

        string strDirPath = Server.MapPath(@"Attachment/" + Session.SessionID);
        try
        {
            if (!Directory.Exists(strDirPath))
            {
                Directory.CreateDirectory(strDirPath);
            }
            StreamWriter ef_File;
            StringBuilder strFile=new StringBuilder();

            ef_File = new StreamWriter(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EF_COMP_FILENAME));
            if(objEFormDetails._EF_COMP != string.Empty)
            {                   
                strFile.Append(objEFormDetails._EF_COMP);
                ef_File.WriteLine(strFile.ToString());
                ef_File.Close();
                sbOnFloppyComp=strFile;
            }

            ef_File  = null;
            strFile=null;

            StringBuilder ef_Cost=new StringBuilder();
            strFile=new StringBuilder();

            ef_File = new StreamWriter(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EF_COST_FILENAME));

            if(objEFormDetails._EF_COST != string.Empty)
            {
                strFile.Append(objEFormDetails._EF_COST);
                ef_File.WriteLine(strFile.ToString());
                ef_File.Close();
                sbOnFloppyCost=strFile;
            }

            GetMemberData();
            GetOtherDirectorsData();
            if(base.IsGuestUser())
            {
                string strEmailBody;
                strEmailBody=GenerateBody();
                string strpackage = strEmailBody;
                GetPackageDetails(strpackage);
            }
            return true;
        }
        catch(Exception ex)
        {
            lblError.Text=ex.Message.ToString();
            return false;
        }
    }

GetMemberData()函数:

private void GetMemberData()
    {
        EFormDetails objEFormDetails = new EFormDetails();
        DataRow drEForm = objEFormDetails.ResultRow;
        if (drEForm != null)
        {
            string strDirPath = Server.MapPath(@"Attachment/" + Session.SessionID);
            eFormation.Business.EFDIR efdir = new eFormation.Business.EFDIR();
            eFormationResult objResult;
            objResult = efdir.LoadEFDIRData(Convert.ToInt64(drEForm[EFORMData.ID_FIELD]), Convert.ToString(drEForm[EFORMData.COMPANYNAME_FIELD]));// + "  " + Convert.ToString(drEForm[EFORMData.LIMITED_FIELD])==DBNull.Value ? string.Empty : drEForm[EFORMData.LIMITED_FIELD])));//give efromid adn comapany name

            if (!Directory.Exists(strDirPath))
            {
                Directory.CreateDirectory(strDirPath);
            }
            StreamWriter swMember;
            StringBuilder sb = new StringBuilder();

            swMember = new StreamWriter(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EFDIR_MEMBER_FILENAME));

            for (int i = 0; i < objResult.ResultData.Tables[0].Rows.Count; i++)
            {
                sb.Append(objResult.ResultTable.Rows[i][0].ToString());
                sb.Append(Environment.NewLine);

            }
            sbOnFloppyMember = sb;
            swMember.WriteLine(sb.ToString());
            swMember.Close();
            sb = null;
            swMember = null;
        }
    }

GetOtherDirectorsData()函数:

private void GetOtherDirectorsData()
    {
        EFormDetails objEFormDetails = new EFormDetails();
        DataRow drEForm = objEFormDetails.ResultRow;
        if (drEForm != null)
        {
            string strDirPath = Server.MapPath(@"Attachment/" + Session.SessionID);
            eFormationResult objResult;
            eFormation.Business.EFODIR objefodir = new eFormation.Business.EFODIR();
            objResult = objefodir.LoadEFODIRData(Convert.ToInt64(drEForm[EFORMData.ID_FIELD]));//change
            if (!Directory.Exists(strDirPath))
            {
                Directory.CreateDirectory(strDirPath);
            }
            StreamWriter swMember;
            StringBuilder sb = new StringBuilder();

            swMember = new StreamWriter(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EFODIR_OTHERDIRECTOR_FILENAME));

            for (int i = 0; i < objResult.ResultData.Tables[0].Rows.Count; i++)
            {
                sb.Append(objResult.ResultTable.Rows[i][0].ToString());
                sb.Append(Environment.NewLine);

            }
            sbOnFloppyOtherDirectors = sb;
            swMember.WriteLine(sb.ToString());
            swMember.Close();
            sb = null;
            swMember = null;
        }
    }

GetPackageDetails()函数:

private void GetPackageDetails(String strBodyContent)
    {
        StreamWriter ef_File;
        StringBuilder strFile = new StringBuilder();

        ef_File = new StreamWriter(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EF_PACKAGE_FILENAME));
        ef_File.WriteLine(strBodyContent);
        ef_File.Close();
        sbOnFloppyComp = strFile;
    }

现在所有上述方法都用于创建文件。 现在,所有这些创建的文件都作为附件添加到

MailAttachment attachment = new MailAttachment(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EF_COMP_FILENAME));
            mEmailMessage.Attachments.Add(attachment);

            MailAttachment attachment = new MailAttachment(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EFDIR_MEMBER_FILENAME));
            mEmailMessage.Attachments.Add(attachment);

            MailAttachment attachment = new MailAttachment(Server.MapPath(@"Attachment/" + Session.SessionID + @"/" + ConstantsData.EF_COST_FILENAME));
            mEmailMessage.Attachments.Add(attachment);

看到任何错误?

我刚刚添加了这两行,因为电子邮件服务器现在已更改为Office 365。

mEmailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", System.Configuration.ConfigurationSettings.AppSettings["SmtpPort"]);
                mEmailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);

1 个答案:

答案 0 :(得分:1)

确保丢弃附件和邮件。否则锁定可能会延迟。

                mail.Attachments.Dispose()
                mail.Dispose()