创建写入文本文件的路径

时间:2016-05-11 18:37:17

标签: vb.net streamwriter

我有响应按钮的代码。此代码有效,但我想知道如何将路径更改为除MyDocuments之外的其他内容。我该怎么做?我是否需要先创建文件路径。例如c:\TextFiles\myfile.txt

Private Sub Button28_Click(sender As Object, e As EventArgs) Handles Button28.Click
    Dim dataLine As String
    Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Using outputFile As New StreamWriter(mydocpath & Convert.ToString("\DataLines.txt"))
        For Each p As PointF In lstECRPairs(106)
            dataLine = p.X & " / " & p.Y
            outputFile.WriteLine(dataLine)
        Next
    End Using
End Sub

2 个答案:

答案 0 :(得分:0)

如果您只是在将在那里写入文件的streamwriter中添加完整路径和文件名,请更正。

Private Sub Button28_Click(sender As Object, e As EventArgs) Handles    Button28.Click
Dim dataLine As String
'YourFilePathAndFileName
Using outputFile As New StreamWriter(c:\TextFiles\myfile.txt)
    For Each p As PointF In lstECRPairs(106)
        dataLine = p.X & " / " & p.Y
        outputFile.WriteLine(dataLine)
    Next
End Using
End Sub

以下行也是获取myDocs的。除非您想要复制到MYDocuments,否则您不需要它。

Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

答案 1 :(得分:0)

我会使用SaveFileDialog方法。

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dataLine As String
    Dim mydocpath As String
    If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
        mydocpath = SaveFileDialog1.FileName
        Using outputFile As New StreamWriter(mydocpath & Convert.ToString("\DataLines.txt"))
            For Each p As PointF In lstECRPairs(106)
                dataLine = p.X & " / " & p.Y
                outputFile.WriteLine(dataLine)
            Next
        End Using
    Else
        ''nothing
    End If
End Sub

我没有添加SaveFileDialog的详细信息您可以在解决方案资源管理器中编辑它们,或者如果您确实需要,我可以编辑我的答案来帮助它。 我还没有能够测试是否应该这样做:)