我有一个单词列表,我想读入一个字符串列表。我在使用Windows Runtime
通常我会使用以下代码:
'load text file in to word list
Using sr As New StreamReader(filePath)
Do While sr.Peek <> -1
WordList.Add(sr.ReadLine.Trim)
Loop
End Using
我正在尝试使用The right way to Read & Write Files in WinRT
中的代码Dim folder = Windows.ApplicationModel.Package.Current.InstalledLocation
folder = folder.GetFolderAsync("Data")
Dim file = folder.GetFileAsync("WordList.txt")
Dim readFile = Windows.Storage.FileIO.ReadTextAsync(file)
但它会在第二行被绊倒,我不知道该怎么办,即使它没有。我已经删除了Await
关键字,因为由于某种原因,它无法在Async
方法上看到GetFolder
属性。
答案 0 :(得分:0)
这是来自Windows应用商店应用开发中心的File Access Sample应用
Private Async Function LoadWords() As Task
Dim fileName As String = "WordListComma.txt"
Dim fileContent As String = ""
Dim file As StorageFile
Dim numBytesLoaded As UInt32
Dim size As UInt64
file = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName)
If file Is Nothing Then Throw New Exception(String.Format("Could not find file {0}", fileName))
Using readStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
Using dataReader As New DataReader(readStream)
size = readStream.Size
If size <= UInt32.MaxValue Then
numBytesLoaded = Await dataReader.LoadAsync(CType(size, UInt32))
fileContent = dataReader.ReadString(numBytesLoaded)
End If
End Using
End Using
End Function
在调用Await
函数时,必须使用Async
关键字,并且只能在使用Async
关键字修饰的方法中存在,因此需要添加到LoadWords()签名。