none
cant delete csv file

    General discussion

  • hi all,

    im creating a csv file from sql querry, after that im sending this csv file with email, after i sent the mail i wanna delete the csv file but it says 'its using by another....' dont know how to resolve this. thanks for help.

    this is my export csv function

    1. Function ExportCsv()
    2. Dim filePath As String = "C:\FULLWork\csv\limit_dosya.csv"
    3. Dim delimiter As String = ";"
    4. Dim sb As New StringBuilder()
    5. For i As Integer = 0 To DataGridView1.Rows.Count - 1
    6. Dim array As String() = New String(DataGridView1.Columns.Count - 1) {}
    7. If i.Equals(0) Then
    8. For j As Integer = 0 To DataGridView1.Columns.Count - 1
    9. array(j) = DataGridView1.Columns(j).HeaderText
    10. Next
    11. sb.AppendLine([String].Join(delimiter, array))
    12. End If
    13. For j As Integer = 0 To DataGridView1.Columns.Count - 1
    14. If Not DataGridView1.Rows(i).IsNewRow Then
    15. array(j) = DataGridView1(j, i).Value.ToString()
    16. End If
    17. Next
    18. If Not DataGridView1.Rows(i).IsNewRow Then
    19. sb.AppendLine([String].Join(delimiter, array))
    20. End If
    21. Next
    22. If File.Exists("c:\FULLWork\csv\limit_dosya.csv") Then
    23. File.Delete("c:\FULLWork\csv\limit_dosya.csv")
    24. Else
    25. File.WriteAllText(filePath, sb.ToString(), System.Text.Encoding.Default)
    26. End If
    27. End Function

    and my send mail function

    1. Function MailGonder()
    2. Dim mail As New MailMessage()
    3. mail.From = New MailAddress("baris.sen@full.com.tr")
    4. mail.[To].Add("kurumsalsatis@full.com.tr,baris.sen@full.com.tr,tevhit.kocabey@full.com.tr")
    5. mail.Subject = "Limit Dosyası"
    6. mail.Body = "Limit Dosyası Ektedir."
    7. Dim smtp As New SmtpClient("EXCHANGE.asya.local")
    8. smtp.Credentials = New Net.NetworkCredential("baris.sen", "password")
    9. Dim oAttch As Net.Mail.Attachment = New Net.Mail.Attachment("c:\FULLWork\csv\limit_dosya.csv")
    10. mail.Attachments.Add(oAttch)
    11. Try
    12. smtp.Send(mail)
    13. Catch exc As Exception
    14. End Try
    15. End Function
    Wednesday, March 21, 2012 8:20 AM

All replies