2012-08-23 33 views
2

我有转移SQL为CSV代码:我通过电子邮件发送的CSV文件C#不能访问该文件,因为它被其他进程

public void CreateCSVFile(DataTable dt, string strFilePath, bool Is_Ordre, int TypeDonne) 
{ 

    //FileStream fs = new FileStream(strFilePath, FileMode.Create); 
    // FileStream fs = File.Create(strFilePath); 

    // Create the CSV file to which grid data will be exported. 
    //StreamWriter sw = new StreamWriter(fs); 
    //StreamWriter sw = new StreamWriter(strFilePath, false); 
    using (var sw = new StreamWriter(strFilePath, false)) 
    { 
     // First we will write the headers. 
     //DataTable dt = m_dsProducts.Tables[0]; 
     int iColCount = dt.Columns.Count; 
     for (int i = 0; i < iColCount; i++) 
     { 
      sw.Write(dt.Columns[i]); 
      if (i < iColCount - 1) 
      { 
       sw.Write(","); 
      } 
     } 
     if (Is_Ordre) sw.Write(", TYP_DONNE"); 

     sw.Write(sw.NewLine); 

     // Now write all the rows. 
     foreach (DataRow dr in dt.Rows) 
     { 
      for (int i = 0; i < iColCount; i++) 
      { 
       if (!Convert.IsDBNull(dr[i])) 
       { 
        sw.Write("\'" + dr[i].ToString().Replace("'", " ").Replace(",", ".") + "\'"); 
       } 
       else 
       { 
        sw.Write("\' \'"); 
       } 
       if (i < iColCount - 1) 
       { 
        sw.Write(","); 
       } 
      } 
      if (Is_Ordre) sw.Write(", \'" + TypeDonne + "\'"); 
      sw.Write(sw.NewLine); 
     }     
     sw.Flush(); 
     sw.Close(); 
     sw.Dispose(); 
    } 
     //fs.Close(); 

} 

之后:

Attachment data_OD = new Attachment(LeSource, MediaTypeNames.Application.Octet); 
Attachment data_LV = new Attachment(LeSource_LV, MediaTypeNames.Application.Octet); 
oEmail.SendingEmail(ClientID, data_OD, data_LV); 

public void SendingEmail(string CodeClient, Attachment dataOD, Attachment dataLV) 
{ 
    try 
    { 
     Pers_Conf oConf = LeConf.Get_Config(CodeClient); 

     MailMessage omail = new MailMessage(); 
     var oSmtp = new SmtpClient("smtp.gmail.com", 587) 
     { 
      Credentials = new NetworkCredential("[email protected]", "xxxxxx"), 
      EnableSsl = true 
     }; 

     omail.From = new MailAddress("[email protected]");    
     omail.To.Add(oConf.EmailAddr); 
     omail.Subject = "xxxxxx_" + CodeClient; 
     omail.Body = CodeClient; 

     omail.Attachments.Add(dataOD); 
     omail.Attachments.Add(dataLV); 

     oSmtp.Send(omail); 
    } 
    catch (Exception excThrown) 
    { 
     throw excThrown; 
    } 

它似乎StreamWriter不能正常关闭文件,我已经与这个尝试:

Try 
    { 
    StreamWriter sw = new StreamWriter(strFilePath, false); 
    } 
    Catch 
    { 
    } 
    Finaly 
    { 
    sw.Flush(); 
    sw.Close(); 
    sw.Dispose(); 
} 

但在finally阻止它不识别sw

有什么想法?

PS:这行代码抛出一个异常,告诉该文件是由其他进程

using (var sw = new StreamWriter(strFilePath, false)) 

是不是因为StreamWriter无法正常关闭,或者是因为发送电子邮件使用此文件关闭正在使用?

+1

你能不能上线,你得到的异常更具体?在最初使用? – Steve

+1

这种情况发生在程序第一次访问这个函数,或者整个过程(build,send)重复使用同一个文件名不止一次? – Steve

+0

它发生在第二次程序访问此功能 – user609511

回答

1

假如你在这个代码超过一次进入。
无论哪种情况,您都需要拨打MailMessage类的MailMsg.Dispose()。 否则,它将保留附件,导致在下次尝试写入文件时锁定文件。

尝试使用using声明还对MAILMESSAGE

 try 
     { 
      Pers_Conf oConf = LeConf.Get_Config(CodeClient); 

      using(MailMessage omail = new MailMessage()) 
      { 
       ..... 
       oSmtp.Send(omail); 
      } 
     } 
     catch (Exception excThrown) 
     { 
      throw excThrown; 
     } 
+0

谢谢...解决我的问题。 – user609511

0

那是因为你的StreamWriter声明超出范围,试试这个来代替:

using (StreamWriter sw = new StreamWriter(strFilePath, false)) 
{ 
} 

这将在编译时添加tryfinally代码为您服务。另外,如果你想指定try块自己做到这一点:

StreamWriter sw; 

try 
{ 
    sw = new StreamWriter(strFilePath, false); 
} 
catch 
{ 
} 
finally 
{ 
    sw.Flush(); 
    sw.Close(); 
    sw.Dispose();   
} 

这将使其范围的finally

编辑

你可以尝试添加using块到你的文件流来创建文件,然后写入该流与StreamWriter为你种已经在做,因为达林指出。

using (FileStream fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write)) 
{ 
    using (StreamWriter sw = new StreamWriter(fs)) 
    { 
     // write your content 

     sw.Flush(); 
     sw.Close(); 
    } 

    fs.Close(); 
} 

如果您使用File.Create,因为它似乎在你的代码(但它注释掉了),那么你不关你的FileStream所以这可能是一个因素创建文件。

+2

这就是他目前有。在'CreateCSVFile'方法中再次查看他的代码。 –

+0

@DarinDimitrov:是的,这是一个懒惰的阅读,我只抓住最后一部分,假设这是他的问题。 –

0

StreamWriter将超出范围。如果您对折射下面...

StreamWriter sw; 
try 
{ 
    sw = new StreamWriter(strFilePath, false); 
} 
Catch 
{ 

} 
finally 
{ 
sw.Flush(); 
sw.Close(); 
sw.Dispose(); 
} 
0

在代码sw你的最后一块没有被识别,因为它超出范围。

请试试这个:

StreamWriter sw; 

try 
{ 
    sw = new StreamWriter(strFilePath, false); 
} 
catch 
{ 
    // Never do an empty catch BTW!  
} 
finally 
{ 
    if (sw != null) 
    { 
    sw.Flush(); 
    sw.Close(); 
    sw.Dispose(); 
    } 
} 

或更好,但使用using语句:

using (StreamWriter sw = new StreamWriter(strFilePath, false)) 
{ 
    // Just do your thing, at the end sw will be cleaned up for you :) 
} 
0

因此,它看起来像你的第一attemopt是合理的(在下面摘要)

using (var sw = new StreamWriter(strFilePath, false)) 
{ 
    sw.Flush(); 
    sw.Close(); 
    sw.Dispose(); 
} 

虽然不需要sw..Dispose(),但它正在使用{}块。

那么你看到的问题究竟是什么?

好吧,我刚刚看到你的编辑。你确定这不是锁定文件的其他东西,可能是附件。我看不到它在声明中的范围,但它可能是挂在文件上的吗?如果您想查询什么是锁,你可以使用的ProcessMonitor ...

http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

相关问题