StreamWriter正在覆盖每一行,我需要它们列出

时间:2011-05-18 11:54:14

标签: vb.net visual-studio-2010

我有以下内容搜索域并列出每个用户。我正在将它们写入文本文件。问题是每个用户名,因为它被覆盖了另一个用户名。

我需要将它们列在另一个之下。我是否错放了使用声明?

再次感谢你们。

Dim de As New DirectoryEntry()
    'Name place to write file to
    Dim strFile As String = "C:\MyFile.txt"
    Dim fileExists As Boolean = File.Exists(strFile)
     'get list of all users on domain and write to file
    de.Path = "WinNT://domain.blah.com"
    For Each d As DirectoryEntry In de.Children()
        Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
            sw.WriteLine(d.Name)
        End Using
    Next

1 个答案:

答案 0 :(得分:8)

您要为列表中的每一行打开一个新的StreamWriter。尝试交换流编写器和循环的创建:

Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
    For Each d As DirectoryEntry In de.Children()
        sw.WriteLine(d.Name)
    Next
End Using