Click here to Skip to main content
16,021,293 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I have written the following code (using snippets) and need to pick up any file that has the .txt file extension in a specific folder. I have tried many code snippets and just cant get it right.

Please can someone modify or help me:confused:


C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows;
using System.Web.Mail;

namespace TfL
{
    class Program
    {
        static void Main(String[] args)
        {
            System.IO.FileInfo info = new System.IO.FileInfo("c:\\Source\\LNS_IS.txt");

            if (File.Exists("c:\\Source\\LNS_IS.txt") && (info.Length > 0))
            {
                MailMessage oMsg = new MailMessage();
                oMsg.From = "Happy@test.com";
                oMsg.To = "246@gmail.com";
                oMsg.Subject = "Unattended Mailbox do not reply";
                oMsg.BodyFormat = MailFormat.Html;

                // ADD AN ATTACHMENT.
                String sFile = @"C:\Source\LNS_IS.TXT";
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);

                oMsg.Attachments.Add(oAttch);

                //SMTP server.
                SmtpMail.SmtpServer = "vmtestmac";
                SmtpMail.Send(oMsg);

                oMsg = null;
                oAttch = null;


                //move the file now email is sent
                string sourceFile = @"C:\Source\LNS_IS.txt";
                string destinationFile = @"C:\Archive\LNS_IS.txt";
                System.IO.File.Move(sourceFile, destinationFile);

                //shows message for testing
                global::System.Windows.Forms.MessageBox.Show("sending File Exists ");

            }
            else
            {
                MailMessage oMsg = new MailMessage();
                oMsg.From = "Happy@test.com";
                oMsg.To = "246@gmail.com";
                oMsg.Subject = "Unattended Mailbox do not reply";
                oMsg.BodyFormat = MailFormat.Html;

                //SMTP server.
                SmtpMail.SmtpServer = "vmtestmac";
                SmtpMail.Send(oMsg);

                oMsg = null;

                //shows message for testing
                global::System.Windows.Forms.MessageBox.Show("sending file not exist");
            }

            //checking if file has any data


            if (info.Length < 1)
            {
                //move the file 
                string sourceFile = @"C:\Source\LNS_IS.txt";
                string destinationFile = @"C:\Baddata\LNS_IS.txt";
                System.IO.File.Move(sourceFile, destinationFile);

             }


        }

    }
}
Posted
Updated 1-Sep-10 2:39am
v2

Try this:

DirectoryInfo dirInfo = new DirectoryInfo(@"c:\MyDir");
string[] files = dirInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
 
Share this answer
 
v2
Comments
Dalek Dave 1-Sep-10 10:24am    
Good Answer.
Sandeep Mewara 1-Sep-10 16:42pm    
Comment from OP: Thanks Again John but I still got an error:-

Cannot implicitly convert type 'System.IO.FileInfo[]' to 'string[]'
:(
Thanks John then I use this code i get two errors:-

best overloaded method match for &#39;System.IO.DirectoryInfo.GetFiles(string, System.IO.SearchOption)&#39; has some invalid arguments

and

Argument '2': cannot convert from 'bool' to 'System.IO.SearchOption'

Any Ideas:confused:
 
Share this answer
 
Comments
#realJSOP 1-Sep-10 9:13am    
Ummm, don't you know what to do? I should have passed SearchOption.TopDirectoryOnly instead of false for the 2nd parameter. I fixed my original response.
Thanks Again John but I still got an error:-

Cannot implicitly convert type &#39;System.IO.FileInfo[]&#39; to &#39;string[]&#39;
:(
 
Share this answer
 
Comments
Sandeep Mewara 1-Sep-10 16:42pm    
Not an answer. Use 'Add Comment' feature to respond to an answer. That will even notify the user.
string[] files = System.IO.Directory.GetFiles(@"c:\MyDir", "*.txt", SearchOption.TopDirectoryOnly);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900