Hi,
I am trying to windows service to read files but it reads even the ones I already read.How can I only read new files? this is the code

// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{

                // do something with fileName
                // Console.WriteLine(fileName);
                                //added

                //read csv file from network
                // String infile;
                // string path = @"C:\URAFILES\PRNS\+fileName+";
                using (StreamReader sr = new StreamReader(fileName))
                {
                    while (sr.Peek() >= 0)
                    {
                        String prnt24 = sr.ReadLine();

Recommended Answers

All 2 Replies

How are you keeping track of the ones you already read?

What about this way:

        privat void MyMethod()
        {
            List<string> results = ReadingFiles(@"C:\MyFolder", "*.txt");
        }

        private List<string> ReadingFiles(string dirPath, string exstension)
        {
            List<string> filesContent = new List<string>();
            StringBuilder sb;
            DirectoryInfo di = new DirectoryInfo(dirPath);
            FileInfo[] files = di.GetFiles(exstension, SearchOption.TopDirectoryOnly);
            foreach (FileInfo file in files)
            {
                using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string line;
                        sb = new StringBuilder();
                        while ((line = sr.ReadLine()) != null)
                        {
                            sb.AppendLine(line);
                        }
                        filesContent.Add(sb.ToString());
                    }
                }
            }
            return filesContent;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.