Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我的列表(字符串)正在另存为system.string(空)_Vb.net_String_Csv - Fatal编程技术网

Vb.net 我的列表(字符串)正在另存为system.string(空)

Vb.net 我的列表(字符串)正在另存为system.string(空),vb.net,string,csv,Vb.net,String,Csv,我正在尝试删除选定的行,然后将其余的保存到文件中。但是,当我保存它时,它会完全清空文件 Console.Write("Please eneter the first name of the student you wish to search for: ") searchfname = Console.ReadLine searchfname = StrConv(searchfname, VbStrConv.ProperCase) Console.Write("P

我正在尝试删除选定的行,然后将其余的保存到文件中。但是,当我保存它时,它会完全清空文件

    Console.Write("Please eneter the first name of the student you wish to search for: ")
    searchfname = Console.ReadLine
    searchfname = StrConv(searchfname, VbStrConv.ProperCase)
    Console.Write("Please enter the second name of the student you wish to search for: ")
    searchsname = Console.ReadLine
    searchsname = StrConv(searchsname, VbStrConv.ProperCase)
    Dim foundItem() As String = Nothing
    Dim foundline As String = Nothing
    Dim fnsearch As String = String.Join(searchfname, searchsname)
    Dim lines As New List(Of String)(File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv"))
    For Each line As String In lines
        If searchfname = item(3) And searchsname = item(4) Then
            Console.WriteLine(line)
            Console.WriteLine()
            Console.WriteLine("Are you sure you wish to delete this record? (y/n)")
        End If
        Dim answer As String
        answer = Console.ReadLine
        If answer = "y" Or answer = "Y" Then
            Console.Clear()
            lines.Remove(line)
            Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
                sw.WriteLine(lines.ToString)
            End Using
        ElseIf answer = "n" Or answer = "N" Then
            staffmenu()
        End If
    Next
方法
List(Of T).ToString
不生成包含集合元素的值。相反,它将只返回类型名

您正在查找的API是
File.writeAllines
。使用它而不是
StreamWriter
使用

File.WriteAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv", lines)

请看代码中的这一行:

sw.WriteLine(lines.ToString)
从该语句中提取
行.ToString
表达式。该表达式的结果是“
System.String
”。您正在告诉流编写器将文本“System.String”写入文件

    Console.Write("Please eneter the first name of the student you wish to search for: ")
    searchfname = Console.ReadLine
    searchfname = StrConv(searchfname, VbStrConv.ProperCase)
    Console.Write("Please enter the second name of the student you wish to search for: ")
    searchsname = Console.ReadLine
    searchsname = StrConv(searchsname, VbStrConv.ProperCase)
    Dim foundItem() As String = Nothing
    Dim foundline As String = Nothing
    Dim fnsearch As String = String.Join(searchfname, searchsname)
    Dim lines As New List(Of String)(File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv"))
    For Each line As String In lines
        If searchfname = item(3) And searchsname = item(4) Then
            Console.WriteLine(line)
            Console.WriteLine()
            Console.WriteLine("Are you sure you wish to delete this record? (y/n)")
        End If
        Dim answer As String
        answer = Console.ReadLine
        If answer = "y" Or answer = "Y" Then
            Console.Clear()
            lines.Remove(line)
            Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
                sw.WriteLine(lines.ToString)
            End Using
        ElseIf answer = "n" Or answer = "N" Then
            staffmenu()
        End If
    Next
要解决此问题,您需要类似以下内容:

Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
    For Each line As String In lines
        sw.WriteLine(line)
    Next line
End Using

我可以从给出的答案和评论中看出这个问题可以得到解决,但我想添加一个替代方法,在文件中写入函数。这样做可能会有所帮助:

Using sw As New StreamWriter(.....)
   sw.WriteLine(Join(lines.ToArray(), Environment.NewLine))
End Using
由于使用VB.Net,这是一个VB.Net特定的解决方案,不能在C#中使用。对于C#,请改用


希望它也有帮助

您没有正确写入文件。改为使用File.writeAllines()。您在这里以及前面的7个问题上得到了几个非常好的答案。当答案有帮助时,你应该开始接受答案。这与你的问题无关,但你是否在搜索JoeBlogs或Joe Blogs。。。。如果是后者,则更改您的联接,使其在searchfname和searchsname之间包含一个空格