From: WD on
Hi,

I've been translating some full .net code onto the compact framwork. I've
had a problem with this:
byte[] fileData = File.ReadAllBytes(<file path and name>);
This isn't in the compact framework library.

I've been looking around and can't seem to find the equivalent of this for
the compact framework.

Any ideas?

Thanks.
--
WD
From: Christopher Fairbairn on
Hi,

"WD" <WD(a)discussions.microsoft.com> wrote in message
news:57088A0E-E0E3-45A9-A73B-7916EACE7E61(a)microsoft.com...
> I've been translating some full .net code onto the compact framwork. I've
> had a problem with this:
> byte[] fileData = File.ReadAllBytes(<file path and name>);
> This isn't in the compact framework library.
>
> I've been looking around and can't seem to find the equivalent of this for
> the compact framework.

The .NET Compact Framework removes a lot of these nice wrapper methods when
it is possible to implement the same behaviour via use of the lower level
functionality offered by the base class libraries. For example you could try
writing your own ReadAllBytes method as shown below (untested):

public static byte[] ReadAllBytes(string path)
{
byte[] buffer;

using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
int offset = 0;
int count = (int)fs.Length;
buffer = new byte[count];
while (count > 0)
{
int bytesRead = fs.Read(buffer, offset, count);
offset += bytesRead;
count -= bytesRead;
}
}

return buffer;
}

Hope this helps,
Christopher Fairbairn


From: WD on
thanks :)

that worked perfectly.
--
WD


"Christopher Fairbairn" wrote:

> Hi,
>
> "WD" <WD(a)discussions.microsoft.com> wrote in message
> news:57088A0E-E0E3-45A9-A73B-7916EACE7E61(a)microsoft.com...
> > I've been translating some full .net code onto the compact framwork. I've
> > had a problem with this:
> > byte[] fileData = File.ReadAllBytes(<file path and name>);
> > This isn't in the compact framework library.
> >
> > I've been looking around and can't seem to find the equivalent of this for
> > the compact framework.
>
> The .NET Compact Framework removes a lot of these nice wrapper methods when
> it is possible to implement the same behaviour via use of the lower level
> functionality offered by the base class libraries. For example you could try
> writing your own ReadAllBytes method as shown below (untested):
>
> public static byte[] ReadAllBytes(string path)
> {
> byte[] buffer;
>
> using (FileStream fs = new FileStream(path, FileMode.Open,
> FileAccess.Read, FileShare.Read))
> {
> int offset = 0;
> int count = (int)fs.Length;
> buffer = new byte[count];
> while (count > 0)
> {
> int bytesRead = fs.Read(buffer, offset, count);
> offset += bytesRead;
> count -= bytesRead;
> }
> }
>
> return buffer;
> }
>
> Hope this helps,
> Christopher Fairbairn
>
>
>