-2

Something's wrong in my code, i want to save listview item into text file using savefiledialog.

I'm getting error "Overload resolution failed because no accessible 'WriteAllLines' accepts this number of arguments."

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles ChromeButton3.Click
        Dim s As New SaveFileDialog
        s.Filter = "text|*.txt"
        s.Title = "Save Your Hits"
        If s.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each myItem As ListViewItem In ListView1.Items
                File.WriteAllLines(myItem.Text & vbNewLine & myItem.SubItems(1).Text & vbNewLine & myItem.SubItems(2).Text & vbNewLine & myItem.SubItems(3).Text & vbNewLine & vbNewLine)
            Next
        End If
    End Sub
Gracia
  • 13
  • 6
  • You have only one argument on your `WriteAllLines`. Check the [WriteAllLines doc](https://learn.microsoft.com/fr-fr/dotnet/api/system.io.file.writealllines?view=netframework-4.8). This fonction need tow or more arguments. – TontonVelu Jan 20 '20 at 13:45
  • its only for having string path, i want to save in any path location – Gracia Jan 20 '20 at 13:50
  • The `SaveFileDialog.FileName` (`s.FileName`) gives you the path of the file. Use that with `File.WriteAllLines`. You can also use the `SaveFileDialog.OpenFile()` method and write to that stream. – Jimi Jan 20 '20 at 17:37
  • You don't save anything with a `SaveFileDialog`. It is simply a UI to allow the user to select a file path to save to. If you want to save to that path then you have to get it from the `SaveFileDialog`, which is what the `FileName` property is for. That property is simply a `String` containing a file path, so you use it in exactly the same way as you would any other `String` containing a file path. If you already know how to save to a hard-coded path then simply replace your hard-coded path with the `FileName` property of the `SaveFileDialog`. – jmcilhinney Jan 20 '20 at 23:37
  • Also, why are you calling `WriteAllLines` in a loop? That makes no sense. The point of `WriteAllLines` is that you get all the lines you want to write in an array first, then you call the method once to write all the lines in one go. You need to rethink the saving logic there. – jmcilhinney Jan 20 '20 at 23:38

1 Answers1

0

Your error

Overload resolution failed

can be corrected by looking at the documentation. It is a good idea to do this with any unfamiliar method. Just google the method followed by "in .net" The first link that came up is https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealllines?view=netframework-4.8 And the first topic in the documentation is overloads. I think you can see that none of the overloads match what you tried to pass.

As was mentioned in comments File.Write All lines isn't really appropriate for your purposes. Instead of making all those lines and a double line between records, Make each row a single line separating each field by a comma. I used a StringBuilder which provides a mutable (changeable) datatype (unlike a String which is immutable). Saves the compiler from throwing away and creating new strings on every iteration.

I appended a new line on each iteration containing an interpolated string. An interpolated string starts with the $. This allows you to directly mix in variables enclosed in { } with the literal characters.

After the loop, you convert the StringBuilder to a String and write to the file with the file name provided by the dialog box.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim s As New SaveFileDialog
    s.Filter = "text|*.txt"
    s.Title = "Save Your Hits"
    If s.ShowDialog = DialogResult.OK Then
        Dim fileName = s.FileName
        Dim sb As New StringBuilder
        For Each myItem As ListViewItem In ListView1.Items
            sb.AppendLine($"{myItem.Text},{myItem.SubItems(1).Text},{myItem.SubItems(2).Text},{myItem.SubItems(3).Text}")
        Next
        File.WriteAllText(fileName, sb.ToString)
    End If
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27