2014-09-18 7 views
-1

나는 .pdf.xls을 포함하지만 숫자는 시간에 따라 달라질 수 있으며이 문제는 me.Now의 주요 문제입니다. 이러한 형식의 파일 모두의 위치와 같이 내 SMTP 메일 코드에 첨부 파일로 추가 ..폴더의 파일을 반복하고 첨부 파일로 추가

Attachment att = new Attachment(sourceDir); 
mail.Attachments.Add(att); 

이이 코드에 첨부 파일로 전술 한 바와 같이 모두를 추가 할 수 괜찮나 하나 개의 파일 첨부입니다 .. 도와주세요 ..

+0

루프를 잊어 버렸습니다()'각 경기를 mail.Attachments.Add''ing 올바른 확장'을 찾고. –

답변

0

다음과 같이 할 수 있습니다.

// Get all the files in the sourceDir 
string[] filePaths = Directory.GetFiles(@"sourceDir"); 

// Get the files that their extension are either pdf of xls. 
var files = filePaths.Where(x=> Path.GetExtension(x).Contains(".pdf") || 
           Path.GetExtension(x).Contains(".xls")); 

// Loop through the files enumeration and attach each file in the mail. 
foreach(var file in files) 
{ 
    var attachment = new Attachment(file); 
    mail.Attachments.Add(attachment); 
} 
0
string[] fileNames = System.IO.Directory.EnumerateFiles("C:\myLocation", "*.xls", System.IO.SearchOption.AllDirectories).ToArray(); 

은 주어진 패턴 인애 주어진 디렉토리와 파일 이름을 retreive 것이다. 원본 경로에서로드하여 메일에 첨부 할 수 있습니다.

편집 :`Directory.GetFiles 통해 .ToArray()

관련 문제