System.IO.File不包含ReadAllBytes C#

时间:2011-06-28 05:54:45

标签: c# windows-phone-7

在C#中我正在为WP7创建简单的facebook应用程序,我遇到了一个问题。

我正在尝试在您可以上传相册或Feed中的图片的部分。

代码:

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

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

错误:

  • System.IO.File不包含ReadAllBytes
  • 的定义

2 个答案:

答案 0 :(得分:5)

你有几个问题。首先,Server.MapPath不会为您提供文件位置(因为您不在Web应用程序中)。但是一旦你知道了你正在寻找的文件路径(在IsolatedStorage中),你就可以做这样的事情来将文件作为字节数组读入:

    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;
    }

答案 1 :(得分:1)

我找到了解决方案。

代码:

string imageName = boxPostImage.Text;
StreamResourceInfo sri = null;
Uri jpegUri = new Uri(imageName, UriKind.Relative);
sri = Application.GetResourceStream(jpegUri);

try
{
    byte[] imageData = new byte[sri.Stream.Length];
    sri.Stream.Read(imageData, 0, System.Convert.ToInt32(sri.Stream.Length));

    FacebookMediaObject fbUpload = new FacebookMediaObject
    {
         FileName = imageName,
         ContentType = "image/jpg"
    };
    fbUpload.SetValue(imageData);


    IDictionary<string, object> parameters = new Dictionary<string, object>();
    parameters.Add("access_token", _AccessToken);
    parameters.Add("source", fbUpload);

    //_fbClient.PostAsync("/"+MainPage._albumId+"/photos", parameters);
    _fbClient.PostAsync("/me/photos", parameters);


    MessageBox.Show("Image has been posted successfully..");
}
catch (Exception error)
{
    MessageBox.Show("Sorry, there's an error occured, please try again.");
}