我在使用Windows 8应用程序时遇到了问题,并从打开的文件中获取了一条信息。在Windows8中读/写文件还有其他方法,但为了能够提取出标准流,我正在尝试是否可以。到目前为止还不是很好。问题是System.IO.BufferedStream不是Windows8应用程序中System.IO的一部分。因此,下面的代码:
(MemoryStream)(await (await installedLocation.GetFileAsync("SOMEFILE")).OpenStreamForWriteAsync());我为精简的代码道歉。上面的代码抛出了前面提到的异常。再一次,问题是我不能创建一个缓冲的流。我想知道有没有办法解决这个问题。
任何帮助都是非常感谢的!
发布于 2012-12-24 04:28:26
这是我在Windows8中用来读写文件的代码。它可以工作,我希望它也能帮助你。
// Read from a file line by line
public async Task ReadFile()
{
try
{
// get the file
StorageFile myStorageFile = await localFolder.GetFileAsync("MyDocument.txt");
var readThis = await FileIO.ReadLinesAsync(myStorageFile);
foreach (var line in readThis)
{
String myStringLine = line;
}
Debug.WriteLine("File read successfully.");
}
catch(FileNotFoundException)
{
}
}
// Write to a file line by line
public async void SaveFile()
{
try
{
// set storage file
StorageFile myStorageFile = await localFolder.CreateFileAsync("MyDocument.txt", CreationCollisionOption.ReplaceExisting);
List<String> myDataLineList = new List<string>();
await FileIO.WriteLinesAsync(myStorageFile, myDataLineList);
Debug.WriteLine("File saved successfully.");
}
catch(FileNotFoundException)
{
}
}发布于 2012-12-24 04:20:13
嗯,是的--你要转换成MemoryStream,但它不是一个MemoryStream。System.IO.BufferedStream似乎是存在的--它只是不公开而已;它是一个你不需要担心的实现细节。但是,您不能将其转换为MemoryStream,因为它不是。
完全不清楚为什么要转换为MemoryStream,但您应该能够使用Stream。如果您需要一个MemoryStream,您必须自己创建一个并将数据复制到其中。
发布于 2012-12-24 04:19:44
根据文档,您应该能够只使用返回的Stream。
http://msdn.microsoft.com/en-us/library/hh582147.aspx
是否有理由将其强制转换为MemoryStream
https://stackoverflow.com/questions/14014376
复制相似问题