首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.IO.File不包含ReadAllBytes C#

System.IO.File不包含ReadAllBytes C#
EN

Stack Overflow用户
提问于 2011-06-28 05:54:44
回答 2查看 5.7K关注 0票数 3

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

我试着做的一部分,你可以上传一张照片在相册或饲料。

代码:

代码语言:javascript
复制
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

的定义。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-28 14:07:56

我找到了解决办法。

代码:

代码语言:javascript
复制
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.");
}
票数 1
EN

Stack Overflow用户

发布于 2011-06-28 06:05:40

你在那有几个问题。首先,Server.MapPath不会给出文件的位置(因为您不在web应用程序中)。但是,一旦您知道了要查找的文件路径(在IsolatedStorage中),就可以执行如下操作,将文件中的数据读入字节数组:

代码语言:javascript
复制
    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;
    }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6502174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档