Search code examples
vb.netwindows-store-appswindows-store

streamwriter in windows store app - write text to file in VB


Does anybody have VB code to emulate Streamwriter for Windows Store?

I know it's been replaced by StorageFolder class but there is no VB sample in MSDN and I can't seem to translate properly from c# examples. Any help would be appreciated. I am just trying to write text (CSV) to a file and save it to the documents folder. In the code below windows store want a stream instead of strPath when I try dim-ing a streamwriter. (been playing with pickerdialog too, but that might be the next hurdle).

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

Solution

  • I was making it too difficult. To write to a file I just needed to use storagefolder and storagefile. I have also included the FileSavePicker in the code (note that filetypechoices is mandatory)

    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