From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
06-02-2009 04:37 AM
Tried to use .NET to read binary file, but results in unexpected runtime error!
Class: System.IO.FileStream
Method: Read
This method requires a numeric array as out-parameter. Further a offset-value and a count-value can be passed to the method. Passing count = 1 everything works fine. If count > 1 runtime error occurs as below:
--------------
Exception has been thrown by the target of an invocation.
Root Exception:Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
Source: mscorlib at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
--------------
Looks like called method cannot handle TestStand numeric array or array isn't passed correctly!
I know that there are a lot of possible workarounds using other languages, libraries, etc. but I'd like to stick on this .NET solution.
Would be nice if someone can give me a hint!
(seq-file attached)
Solved! Go to Solution.
06-04-2009 04:41 AM
06-04-2009 04:46 AM
@ AndreasS:
This is exactly what I already mentioned in my message (see above).
But I'd like to read more than one byte from file!
rgds.
06-04-2009 05:02 AM
Sorry, missed the "count > 1" part.
The Run-time error is passed back from the .NET assembly and has nothing to do with TestStand.
I'm not a .NET expert, but I think you get the .NET error because you are missing some steps. Here a Visual Basic example from:http://msdn.microsoft.com/de-de/library/system.io.filestream.read.aspx
Imports System
Imports System.IO
Class FSRead
Public Shared Sub Main()
'Create a file stream from an existing file.
Dim fi As New FileInfo("c:\csc.txt")
Dim fs As FileStream = fi.OpenRead()
'Read 100 bytes into an array from the specified file.
Dim nBytes As Integer = 100
Dim ByteArray(nBytes) As Byte
Dim nBytesRead As Integer = fs.Read(ByteArray, 0, nBytes)
Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString())
End Sub 'Main
End Class 'FSRead
Regards,
06-04-2009 10:12 AM
Bestware,
Try this sequence. Put both files in the same directory. I guess if it works with text files, it should also work with binary files too.
Probably a better solution would be to use ReadByte instead of read, inside a loop.
I agree with you that this method cannot handle Teststand array.
Regards
06-04-2009 08:55 PM
06-05-2009 02:05 AM
Thank you all!
I'm wondering why it's not possible to define certain datatype (e.g. array of bytes) to pass to .NET methods, as
it's possible to define e.g. array of bytes to pass to C/C++ DLL!