Reputation: 1
So, basically I'm doing a "save as" button and when the file is been saved, I want the path from the file that is been saved goes to another txt file tottaly different.
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
SaveFileDialog1.ShowDialog()
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
End Sub
Upvotes: 0
Views: 612
Reputation: 84
To add onto @Spyros P's answer is, I would store SaveFileDialog1.ShowDialog()
into a variable because if you cancel or X out of the Save window
it will still continue onto the saving of the file. Possibly do something like this:
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
Dim temp = SaveFileDialog1.ShowDialog()
If temp = False Then Return
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
'new code
'get new filename by appending .tmp to the original filename
Dim tmpFilePath As String = SaveFileDialog1.FileName & ".tmp"
IO.File.WriteAllText(tmpFilePath, SaveFileDialog1.FileName)
End Sub
Over all @Spyros P. is correct, all I changed was I added the variable for the SaveFileDialog1.ShowDialog
Upvotes: 1
Reputation: 296
So, your problem is writing to a different file as well? Or somehow, returning two different filenames from the SaveFileDialog? If it's the latter, I don't believe this can be done.
If it's the former, you already know how to write to text files, so this answer seems redundant. Still, the following code (which assumes that the "totally different txt file" is named by appending ".tmp" to the original filename) will save the original path to the second file:
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
SaveFileDialog1.ShowDialog()
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
'new code
'get new filename by appending .tmp to the original filename
Dim tmpFilePath As String = SaveFileDialog1.FileName & ".tmp"
IO.File.WriteAllText(tmpFilePath, SaveFileDialog1.FileName)
End Sub
Upvotes: 1