0

有人有 VB 代码来模拟 Windows Store 的 Streamwriter 吗?

我知道它已被 StorageFolder 类取代,但 MSDN 中没有 VB 示例,我似乎无法从 c# 示例中正确翻译。任何帮助,将不胜感激。我只是想将文本(CSV)写入文件并将其保存到文档文件夹中。在下面的代码中,当我尝试调暗流写入器时,windows store 想要一个流而不是 strPath。(也一直在玩pickerdialog,但这可能是下一个障碍)。

Dim strpath As String = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary & "\" & strFileName
    'Build String for file*******************
    Dim swExport As StreamWriter = New StreamWriter(strpath)

    swExport.Flush()

    For x = 0 To intCount - 1
        strLine = "WriteSomeText"
        swExport.WriteLine(strLine)
    Next x
4

2 回答 2

0

可能最简单的方法是使用 a MemoryStreamif you like StreamWriter,例如:

Dim sessionData As New MemoryStream()
' TODO: stage data in sessionData
Dim swExport As StreamWriter = New StreamWriter(sessionData)

swExport.Flush()

For x = 0 To intCount - 1
    strLine = "WriteSomeText"
    swExport.WriteLine(strLine)
Next x

Dim file As StorageFile = await ApplicationData.Current.RoamingFolder.CreateFileAsync("towns.json", CreationCollisionOption.ReplaceExisting)
Using (fileStream As Stream = await file.OpenStreamForWriteAsync())
    sessionData.Seek(0, SeekOrigin.Begin)
    await sessionData.CopyToAsync(fileStream)
    await fileStream.FlushAsync()
End Using
于 2013-08-25T16:29:20.960 回答
0

我太难了。要写入文件,我只需要使用 storagefolder 和 storagefile。我还在代码中包含了 FileSavePicker(请注意,filetypechoices 是强制性的)

Private Async Function btnExport_Click(sender As Object, e As RoutedEventArgs) As Task
    'Calls Filepicker to determine location
    'Calls Sqlite to select ALL
    'Creates CSV file to be saved at location chosen


    'save to file
    Dim intCount As Integer = "5"
    Dim x As Integer
    Dim strLine As String               'hold each line for export file


    'Create FileName based on date
    Dim strDate As String = Date.Today.ToString("MMddyyyy")
    Dim strFileName As String = "Export" & strDate & ".csv"

    ' Configure save file dialog box 
    Dim dlgPicker As New Windows.Storage.Pickers.FileSavePicker

    'add types for picker (manditory field)
    Dim types = New List(Of String)
    types.Add(".csv")
    types.Add(".txt")

'set picker parameters
    dlgPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads
    dlgPicker.SuggestedFileName = strFileName '"Document"               ' 
    dlgPicker.FileTypeChoices.Add("CSV/TXT", types)             'manditory 
    dlgPicker.DefaultFileExtension = ".csv"                 'Filter files by extension

    dlgPicker.CommitButtonText = "Save"

    ' Show save file dialog box 
    Dim SaveCSV = Await dlgPicker.PickSaveFileAsync()

    '************************get data************
    Dim sbExport As Text.StringBuilder = New Text.StringBuilder
    sbExport.AppendLine(strHeader)
    For x = 0 To intCount - 1
        strLine = "Get the text you want to write here"
        sbExport.AppendLine(strLine)
    Next x
    '************************************
    'write data to file
    Await FileIO.WriteTextAsync(SaveCSV, sbExport.ToString)


    Dim mb As MessageDialog = New MessageDialog("Done")
    Await mb.ShowAsync()


End Function
于 2013-08-31T12:24:27.847 回答