Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.File does not contain ReadAllBytes C#

In C# I am creating simple facebook application for WP7 and I came across a problem.

I'm trying to do the part where you can upload a picture in the album or feed.

Code:

FacebookMediaObject facebookUploader = new FacebookMediaObject { FileName = "SplashScreenImage.jpg", ContentType = "image/jpg" };

var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~") + facebookUploader.FileName);
facebookUploader.SetValue(bytes);

Error:

  • System.IO.File does not contain a definition for ReadAllBytes
like image 957
Puzo Avatar asked Nov 30 '25 23:11

Puzo


1 Answers

You've got a couple of problems there. First off, Server.MapPath isn't going to give you the file location (since you're not in a web application). But once you do know the file path you're looking for (in IsolatedStorage), you can do something like this to read in the file as a byte array:

    public byte[] ReadFile(String fileName)
    {
        byte[] bytes;
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream file = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
            {
                bytes = new byte[file.Length];

                var count = 1024;
                var read = file.Read(bytes, 0, count);
                var blocks = 1;
                while(read > 0)
                {
                    read = file.Read(bytes, blocks * count, count); 
                    blocks += 1;
                }
            }
        }
        return bytes;
    }
like image 98
E.Z. Hart Avatar answered Dec 02 '25 12:12

E.Z. Hart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!