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();

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;
        }
This article has been dead for over six months. Start a new discussion instead.