locked
check if file's created time is less than 5 minutes RRS feed

  • Question

  • User-1660589204 posted

    Hi, 

    I am new to .Net coding and I have a situation to check and take all the files from a folder that are created in last 5 minutes. My code is not good and is not working. It returns null even if I have created the file there. I have no compile or runtime errors so, I obviously did bad code.  Please check my code and help me:

    public string FileChecker()
    {
         if (!File.Exists(path))
         {
             return "File does not exist int the path";
         }
         else
         {
             if ((DateTime.Now - File.GetCreationTime(path)) <= TimeSpan.FromMinutes(5))
             {
                 string result = Path.Combine(path, filename);
                 return result;
             }
         }

    Sunday, November 8, 2020 3:14 PM

Answers

  • User-939850651 posted

    Hi ddesarajubyc,

    According to your description, I modified and tested this part of the code. The File.GetCreationTime(path) function actually works. 

    So I am not sure if you have other modifications.

    This is my test:

    class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
               var result = p.FileChecker();
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            public string FileChecker()
            {
                string result = string.Empty;
                string path = "D:\\TestFolder\\test.txt";
                //string filename = "test.txt";
                if (!File.Exists(path))
                {
                    result = "File does not exist int the path";
                }
                else
                {
                    if ((DateTime.Now - File.GetCreationTime(path)) <= TimeSpan.FromMinutes(5))
                    {
                        //result = Path.Combine(path, filename);
                        result = File.GetCreationTime(path).ToString();
                    }
                    else {
                        result = "There are no files created in the last 5 minutes";
                    }
                }
                return result;
            }
        }

    Result:

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 10:25 AM

All replies

  • Sunday, November 8, 2020 3:37 PM
  • User-939850651 posted

    Hi ddesarajubyc,

    According to your description, I modified and tested this part of the code. The File.GetCreationTime(path) function actually works. 

    So I am not sure if you have other modifications.

    This is my test:

    class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
               var result = p.FileChecker();
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            public string FileChecker()
            {
                string result = string.Empty;
                string path = "D:\\TestFolder\\test.txt";
                //string filename = "test.txt";
                if (!File.Exists(path))
                {
                    result = "File does not exist int the path";
                }
                else
                {
                    if ((DateTime.Now - File.GetCreationTime(path)) <= TimeSpan.FromMinutes(5))
                    {
                        //result = Path.Combine(path, filename);
                        result = File.GetCreationTime(path).ToString();
                    }
                    else {
                        result = "There are no files created in the last 5 minutes";
                    }
                }
                return result;
            }
        }

    Result:

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 10:25 AM