FileStream.Read Problem with offset value

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-weight:bold i want to do a FileStream.Read on a file for my web service. Sample code below:-

[WebMethod]
    public Byte[] GetDocument2(string DocumentName, int offset, int length)
    {
        string strdocPath;
        strdocPath = "e:\" + DocumentName;

        FileStream objfilestream = new FileStream(strdocPath, FileMode.Open, FileAccess.Read);
        int len = (int)objfilestream.Length;
        Byte[] documentcontents = new Byte[length+1];
        objfilestream.Read(documentcontents, offset, length);
        objfilestream.Close();

        return documentcontents;
    }

<span style="font-weight:bold from the sample code above, if offset is 0 and length is any value it will runs without a problem<br style="font-weight:bold <span style="font-weight:bold but if i pass in offset with a value of more than 1 then it generate the exception below:-
<pre>System.ArgumentException: 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.
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at Service.GetDocument2(String DocumentName, Int32 offset, Int32 length) in c:inetpubwwwrootWebSite1App_CodeService.cs:line 94[/code]


View the full article
 
Back
Top