2016-10-20 24 views
-4

エラーが発生しました。 'IOException:プロセスが別のプロセスで使用されているためファイルファイルパスにアクセスできません。理由を把握する。私はもっ​​と大きなプロジェクトでそれを手に入れていましたが、この投稿には膨大なコードブロックが必要でしたので、私はそれを打ち明かしましたので、愚かなクラス名と変数名を許してください。whileループでエラーが発生しました。ファイルとThread.Sleep(1000)

あなたはそれがそのすべての接続を閉じ、すべてのリソースは、それが開いている可能性があることを取り扱う解放しますように(例えば、添付ファイルへのハンドル) MailMessageを配置する必要が
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Net.Mail; 
using System.Threading; 

namespace Test_Bench 
{ 
    public class poo 
    { 
     public void penises() 
     { 
      StreamWriter chinatown; 
      chinatown = new  StreamWriter("C:\\Users\\eggroll\\Desktop\\file.txt"); 
      chinatown.Write("SUP BOIS"); 
      chinatown.Close(); 
     } 
    } 
    public class hello 
    { 
     public void eggroll() 
     { 
      MailAddress senderAddress = new MailAddress("[email protected]"); 

      MailAddress receiverAddress = new MailAddress("[email protected]"); 

      MailMessage mail = new MailMessage(senderAddress, receiverAddress); 

      System.Net.Mail.Attachment attachment; 
      attachment = new  System.Net.Mail.Attachment("C:\\Users\\example\\Desktop\\file.txt"); 
      mail.Attachments.Add(attachment); 

      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 

      smtp.Credentials = new NetworkCredential(
       "[email protected]", "example"); 
      smtp.EnableSsl = true; 
      smtp.Send(mail); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var chicken = new poo(); 
      var hello = new hello(); 
      while (true) 
      { 
       chicken.penises(); 
       hello.eggroll(); 
       Thread.Sleep(1000); 
      } 
     } 
    } 
} 
+0

あなたの現在の投稿コードでこの例外が発生しますか? – nvoigt

+0

名前は本当に愚かです。彼らの名前にもう少し意味をつけることはできませんか?それは、難読化されたコードのようなビットを読み取ります。あなたは 'IOException'をどこで手に入れましたか?ファイルを書き込んだとき、または添付ファイルを送信したときにそれが起こりますか? – haindl

答えて

1

public class hello 
{ 
    public void eggroll() 
    { 
     MailAddress senderAddress = new MailAddress("[email protected]"); 

     MailAddress receiverAddress = new MailAddress("[email protected]"); 

     using (MailMessage mail = new MailMessage(senderAddress, receiverAddress)) 
     { 
      System.Net.Mail.Attachment attachment; 
      attachment = new  System.Net.Mail.Attachment("C:\\Users\\example\\Desktop\\file.txt"); 
      mail.Attachments.Add(attachment); 

      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 

      smtp.Credentials = new NetworkCredential(
       "[email protected]", "example"); 
      smtp.EnableSsl = true; 
      smtp.Send(mail); 
     } 
    } 
} 

これは特に重要ですメールの添付ファイルを送信しているとき。

関連する問題