Search code examples
vb.netpdfindexoutofboundsexceptionlast-modified

GetFileInfo() and LastWriteTime() is giving an error : Index was outside the bounds of the array


I have following code spinet:

UPDATE

(code above this code is creation of pdf file)
Dim PdfFileInfo As FileInfo
Dim PdfModificationTime As Date
If (FileIO.FileSystem.FileExists(strPdfFileName)) Then
   PdfFileInfo = FileIO.FileSystem.GetFileInfo(strPdfFileName)
   PdfModificationTime = PdfFileInfo.LastWriteTime()
End If

File is exist, but when I use GetFileInfo() and LastWriteTime() it gives an error : "Index was outside the bounds of the array"

I tried lot of things, but no luck... :(

SOLVED

Issue has been solved, answered separately !


Solution

  • Two important points are Here..

    1. possibility of error at those lines which I have mentioned in the question

    2. error : Index was outside the bounds of the array

    Lets talk about 1st:


    Many told that error at those lines could not be possible, I thought that too while writing a code at first, but got an error which was showing in logs.

    Then I have read documentation about system function GetFileInfo().

    What they say, this function gives information about file which system has "cashed" in its memory, system does this cashing periodically.

    Now just think, system just cashed the file system information in its memory, now you have created a file and written something in a file and called the function GetFileInfo().

    Saw the problem? See when you called a function GetFileInfo(), system doesn't have even a file object cashed in the memory. So we get fileinfo object null here and when you try to get LastWriteTime() of null object,here system throws an exception.

    Solution : I have put null check using while loop on fileInfo object that is PdfFileInfo in code (and yes this problem is been tested and solved .... :) )


    Now talk about 2nd:

    The code referred in a question was not in a try block so any exception thrown by this function block got received in calling function. Here calling function resolved this exception in its own way and thrown an exception "Index was outside the bounds of the array" which was relevant for calling function but not reflected actual exception thrown by GetFileInfo().

    Solution : put this code block in try-catch block

    Thanks for all comments and discussions... :) !