Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
System.IO.File不包含ReadAllBytes C#_C#_Windows Phone 7 - Fatal编程技术网

System.IO.File不包含ReadAllBytes C#

System.IO.File不包含ReadAllBytes C#,c#,windows-phone-7,C#,Windows Phone 7,在C#中,我为WP7创建了一个简单的facebook应用程序,遇到了一个问题 我试着做的部分,你可以上传相册或饲料中的图片 代码: 错误: System.IO.File不包含ReadAllBytes的定义 您遇到了一些问题。首先,Server.MapPath不会提供文件位置(因为您不在web应用程序中)。但是,一旦知道要查找的文件路径(在IsolatedStorage中),就可以执行以下操作以字节数组的形式读入文件: public byte[] ReadFile(String file

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

我试着做的部分,你可以上传相册或饲料中的图片

代码:

错误:

  • 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=新Uri(imageName,UriKind.Relative);
sri=Application.GetResourceStream(jpegUri);
尝试
{
字节[]图像数据=新字节[sri.Stream.Length];
sri.Stream.Read(imageData,0,System.Convert.ToInt32(sri.Stream.Length));
FacebookMediaObject fbUpload=新建FacebookMediaObject
{
FileName=imageName,
ContentType=“image/jpg”
};
fbUpload.SetValue(图像数据);
IDictionary参数=新字典();
添加(“访问令牌”,“访问令牌”);
参数。添加(“源”,fbUpload);
//_fbClient.PostAsync(“/”+主页._albumId+“/照片”,参数);
_fbClient.PostAsync(“/me/photos”,参数);
MessageBox.Show(“图像已成功发布…”);
}
捕获(异常错误)
{
Show(“抱歉,发生了错误,请重试。”);
}

我会对
隔离存储文件流使用“using”块,并在循环中读取,而不是假设对
读取的单个调用将获得所有内容。更新为对流使用using块-感谢您的建议。。。。aannand再次更新为在循环中读取,因为我刚刚意识到文件的长度是Int64,可能超过了read一次读取整个文件的能力。乔恩,谢谢你让我保持诚实。@E.Z.哈特:这总是会读到数组的开头。你需要不断更新你正在阅读的内容:)谢谢大家的回答。。我将尝试你的例子。+1作为一个包含代码的单独问题发布。
    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.");
}