windows phone 7 - System.IO.File does not contain ReadAllByt
本文关键字:does not contain ReadAllByt File IO phone System windows | 更新日期: 2023-09-27 18:00:29
在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.");
}