Windows商店应用程序中的流写器 - 在VB中将文本写入文件

时间:2013-08-25 14:54:44

标签: vb.net windows-store-apps windows-store

是否有人使用VB代码模拟Streamwriter for Windows Store?

我知道它已被StorageFolder类所取代,但MSDN中没有VB样本,我似乎无法从c#示例中正确翻译。任何帮助,将不胜感激。我只是尝试将文本(CSV)写入文件并将其保存到文档文件夹。在下面的代码中,当我尝试调整一个streamwriter时,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

2 个答案:

答案 0 :(得分:0)

如果您喜欢MemoryStream,最简单的方法可能是使用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

答案 1 :(得分: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