簡體   English   中英

System.IO.File 不包含 ReadAllBytes C#

[英]System.IO.File does not contain ReadAllBytes C#

在 C# 中,我正在為 WP7 創建簡單的 facebook 應用程序,但遇到了一個問題。

我正在嘗試做您可以在相冊或提要中上傳圖片的部分。

代碼:

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 的定義

你有幾個問題。 首先,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;
    }

我找到了解決方案。

代碼:

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.");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM