System.IO.IOException: The process cannot access the file '' because it is being used by another process.

I am getting the above mentioned error. My code is as below and the stack trace also follows. I am not trying to open the file or copy or move, i just want to attach it to an email.  

        Dim _today As Date
        Dim _dayNumber As Integer
        Dim _wcDayNumber As Integer
        Dim _stepDownVal As Integer
        Dim _negated As Integer
        Dim _wcDay As Date
        Dim wc As String

        Dim _filename As String
        Dim _foldername As String

        _today = Date.Today
        _dayNumber = Date.Today.DayOfWeek
        _wcDayNumber = 1
        _stepDownVal = _dayNumber - _wcDayNumber

        _negated = "-" & _stepDownVal
        _wcDay = _today.AddDays(_negated)
        wc = _wcDay.ToString("dd MMM yy")
        _foldername = _wcDay.ToString("MMMMM yy")

        _filename = "\\share\folder\" & _foldername & "\wk com " & wc & ".xls"

        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("")
        mail.To.Add("")

        'set the content
        mail.Subject = "Subject"
        mail.Body = "Body"
        mail.IsBodyHtml = True

        System.Threading.Thread.Sleep(5000)
        'add an attachment from the filesystem
        mail.Attachments.Add(New Attachment(_filename))

        'send the message
        Dim smtp As New SmtpClient("server")
        smtp.Send(mail)

STACK TRACE

[IOException: The process cannot access the file '\\seabrook-rdc\SHIFTS\October 08\wk com 06 Oct 08.xls' because it is being used by another process.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +2056741
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +114
   System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) +75
   System.Net.Mail.Attachment..ctor(String fileName) +48
   _Default.emailer() +464
   _Default.Page_Load(Object sender, EventArgs e) +7
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
0
takwirira
10/9/2008 1:17:27 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

7 Replies
3570 Views

Similar Articles

[PageSpeed] 18
Get it on Google Play
Get it on Apple App Store

I am getting the above mentioned error. My code is as below and the stack trace also follows. I am not trying to open the file or copy or move, i just want to attach it to an email.

<CODE> 

Dim _today As Date

Dim _dayNumber As Integer

Dim _wcDayNumber As Integer

Dim _stepDownVal As Integer

Dim _negated As Integer

Dim _wcDay As Date

Dim wc As String

Dim _filename As String

Dim _foldername As String

_today = Date.Today_dayNumber = Date.Today.DayOfWeek

_wcDayNumber = 1

_stepDownVal = _dayNumber - _wcDayNumber

_negated =
"-" & _stepDownVal

_wcDay = _today.AddDays(_negated)

wc = _wcDay.ToString(
"dd MMM yy") _foldername = _wcDay.ToString("MMMMM yy")

'_filename = Server.MapPath(_foldername & "\wk com " & wc & ".xls")

_filename = "\\seabrook-rdc\SHIFTS\" & _foldername & "\wk com " & wc & ".xls"

'create the mail message

Dim mail As New MailMessage()

'set the addresses

mail.From = New MailAddress("takwirirar@seabrookcrisps.com") mail.To.Add("takwirira@gmail.com")

'set the content

mail.Subject = "Shifts"

mail.Body = "Report"

mail.IsBodyHtml = True

System.Threading.Thread.Sleep(5000)

'add an attachment from the filesystem

mail.Attachments.Add(New Attachment(_filename))

'send the message

Dim smtp As New SmtpClient("seabrook-rdc")

smtp.Send(mail)

</CODE>

0
takwirira
10/9/2008 1:27:35 PM

Try to send the file from your local drives such as C:. If it works, that means you need to deal with the security issue to access the network drive.

Assign the right access to the network drive "\\seabrook-rdc\SHIFTS\" & _foldername

By the way, if it is a web application, you can try put the following line in your web.config file

<identity impersonate="true" />


"Object reference not set to an instance of an object"
0
che3358
10/9/2008 1:57:35 PM

Are u creating the file before sending it as attachement? If yes make sure that the streamwritter is being close

 


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
10/9/2008 2:04:00 PM

I have tried from the local drives and it gave the same error. The security rights are correct because I have given the internet user and Everyone read access. all the web page does is send an attachemnt

0
takwirira
10/9/2008 2:54:30 PM

The file is already sat on a drive somewhere, its not being created or anything like that

0
takwirira
10/9/2008 2:55:09 PM

The code that works is as below, however now the process locks the file and noone can then access it. I have to manually go on the server and release the lock everytime. Is there another line of code that I need to release the lock on the file?

 

Imports System.Net.Mail
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        emailer()
    End Sub
    Function emailer() As Integer
        Dim _today As Date
        Dim _dayNumber As Integer
        Dim _wcDayNumber As Integer
        Dim _stepDownVal As Integer
        Dim _negated As Integer
        Dim _wcDay As Date
        Dim wc As String

        Dim _filename As String
        Dim _foldername As String

        _today = Date.Today
        _dayNumber = Date.Today.DayOfWeek
        _wcDayNumber = 1
        _stepDownVal = _dayNumber - _wcDayNumber

        _negated = "-" & _stepDownVal
        _wcDay = _today.AddDays(_negated)
        wc = _wcDay.ToString("dd MMM yy")
        _foldername = _wcDay.ToString("MMMMM yy")

        '_filename = Server.MapPath(_foldername & "\wk com " & wc & ".xls")
        _filename = "D:\Public\Production\Shifts etc\24 HOUR CONTINENTALS\" & _foldername & "\wk com " & wc & ".xls"

        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("")
        mail.To.Add("")
        mail.To.Add("")

        'set the content
        mail.Subject = "Shifts"
        mail.Body = "Report"
        mail.IsBodyHtml = True

        System.Threading.Thread.Sleep(5000)
        'add an attachment from the filesystem
        mail.Attachments.Add(New Attachment(_filename))

        'send the message
        Dim smtp As New SmtpClient("seabrook-rdc")
        smtp.Send(mail)
    End Function
End Class
 
0
takwirira
10/14/2008 8:51:19 AM

mail.Attachments.Dispose() sorted it guys !! Thanks

0
takwirira
10/14/2008 10:50:52 AM
Reply: