locked
FileInfo length not returning correct value? RRS feed

  • Question

  • Hello Guys

    I have following code that based on how big the file is comes up with the milli seconds (int) that my process would wait for. For some reason some of the file lengths are coming out to zero. Can someone point out if there is flaw in the logic that causes this? I think the fileInfo length is zero because in my event viewer I see that 0 ms was allocated for this process. If my understanding of math.ceiling is correct, this is possible only if length is zero ( for the code below ).

    Please help!!!

    private int GetTimeForCurrentFile(string currentFileToProcess)
        {
          long fileSize = new FileInfo(System.IO.Path.Combine(FSWatcher.Path, currentFileToProcess)).Length;
          int multiplyFactor = (int)Math.Ceiling(Convert.ToDouble(fileSize) / 1000000);
          return multiplyFactor * 10 * 60000;
        }
    
    This method is getting called from the following code:
    
          Process process = new Process();
            try
            {
              process.StartInfo.FileName = ProgramToStart;
              process.StartInfo.Arguments = currentFileToProcess;
              process.Start();
              int timeToWait = GetTimeForCurrentFile(currentFileToProcess);
              string logEntry = string.Format("File: {0} Created. Total time allocated {1} ms.", currentFileToProcess, timeToWait);
              EventLog.WriteEntry("Directory Monitor", logEntry);
              process.WaitForExit(timeToWait);
              if (process.HasExited == false)
              {
                EventLog.WriteEntry("Directory Monitor", "File: " + currentFileToProcess + " not processed as it took more than allocated time. The process was terminated.");
                process.Kill();
              }
    

    DV
    Tuesday, May 25, 2010 9:35 PM

Answers

  • Yes, your assumption about Math.Ceiling is correct.

    If you see that the file does exist and is not empty, my guess is that your code inspected the file while it was still being written. Buffering can make the size appear to be zero for a while.

    HTH
    --mc

    • Marked as answer by tinuverma Wednesday, May 26, 2010 3:38 PM
    Tuesday, May 25, 2010 10:15 PM