write / read from a text file
- Hi
I have a problem read / write from a text file
When i read from the text file using
Using st As StreamReader = New StreamReader("musik.txt")And when i write to the text file using:
My.Computer.FileSystem.WriteAllText("musik.txt", OpenFileDialog1.FileName, True)Is it not the same place i read and write to , so how can I besure to read/write to this text file, the text file shall bee in my application.
ALK
Answers
I'm not in front of a PC at the moment.
If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
Mark the best replies as answers. "Fooling computers since 1971."
Rudy,
Good point.
@Alvin ->
Try something like this:
Dim baseFolder As String = Application.StartupPath Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Button to open the text file OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog1.FilterIndex = 1 OpenFileDialog1.FileName = baseFolder & "\*.txt" If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ' ----------------------- ' Start the rest here ... ' ----------------------- End If End Sub
- Marked As Answer byAlvin Kuiper Friday, November 06, 2009 9:19 AM
All Replies
- Please Match Your Problem To here
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/43157f37-9211-4ec8-a240-4b22937e3a28
and here:
Check text saving and Opening Method
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/37323a3e-ae37-45d7-b3e8-6ef4115e01a0
www.shariqdon.media.officelive.com I do not see that you have actually read the file, and also...your write does not reference anything to write, except a filename string from your OpenDialog, which surely is not your intent. Here is a snippet both reads a file, and appends to the same file:
' Array to hold strings from the file Dim alltext() As String ' This is an easier way to get all of the strings in the file. alltext = File.ReadAllLines("musik.txt") ' This will append the string to the end of the file. My.Computer.FileSystem.WriteAllText("musik.txt", _ "This is a literal string, or a string variable." & vbCrLf, _ True)- Hi
I can see that I have explain my self bad.
I try again
I have this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim alltext() As String alltext = File.ReadAllLines("musik.txt") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim text As String OpenFileDialog1.ShowDialog() text = OpenFileDialog1.FileName My.Computer.FileSystem.WriteAllText("musik.txt", _ text, True) End SubWhen i read the file musik.txt (button1) I read from my application/bin/debug/musik.txt
When i Write (button2) I write to a file musik.txt there comes in the folder where Openfiledialog have found the file
So if i in openfiledialog select a file in c:\tut\myfile.doc
then I write in a txt fil musik.txt in the folder c:\tut
And what i want is to write in the same file i read from /bin/debug/musik.txt
ALK Is it not the same place i read and write to , so how can I besure to read/write to this text file, the text file shall bee in my application.
You new post doesn't explain your problem. The file should be in the same location (your debug folder) when you read and write it. Your code doesn't show what you write to the file or if you do anything with what you read from the file.
I take that back. Make "musik.txt", Application.StartupPath + "\musik.txt"- Stop using the OpenFileDialog.
It will read/write to the current output folder by default, without a path name on the file at all.
Mark the best replies as answers. "Fooling computers since 1971." - Perhaps you should keep the full path to the file when you first use it...Have a look at this:
Dim fi As New FileInfo("musik.txt") MsgBox(fi.FullName, MsgBoxStyle.Information)- Edited byjinzai Thursday, November 05, 2009 7:00 PMtypos
- Well i will give u a very simple coding just replace the names. to view the file u need a multi line text box. then drag and drop 2 buttons name one as btnview and second as btnsave. btnview to view the file and btnsave to save the file. without selecting the file from the dialog box u can open and read the file directly. and wen u will save it will save directly. Now on the top of yr coding screen above the public class add this code.
(Imports system.I.O.)
dnt put in brakets. and then double click on btnview. and put this coding.
Dim myReader As IO.StreamReader
myReader = IO.File.OpenText("musik.txt")
txtbox1.Text = myReader.ReadToEnd()
myReader.Close()
The codes above will read yr file and now to save it. In the text box u have viewed the file u can edit it in the same text box. means delete or write a few lines into the text box... Now the coding for btnsave.
Dim mywriter As IO.StreamWriter
Dim myreader As IO.StreamReader
mywriter = IO.File.CreateText("musik.txt")
mywriter.WriteLine(txtbox1.Text)
mywriter.Flush()
mywriter.Close()
myreader = IO.File.OpenText("musik.txt")
txtbox1.Text = myReader.ReadToEnd()
myrecordreader.Close()
The codes above will create a new music text file then it will write all the txt in the text box 1 and then it will replace it with the ancient one. and then will display the new one... Hi
I can see that I have explain my self bad.
I try again
I have this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim alltext() As String alltext = File.ReadAllLines("musik.txt") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim text As String OpenFileDialog1.ShowDialog() text = OpenFileDialog1.FileName My.Computer.FileSystem.WriteAllText("musik.txt", _ text, True) End SubWhen i read the file musik.txt (button1) I read from my application/bin/debug/musik.txt
When i Write (button2) I write to a file musik.txt there comes in the folder where Openfiledialog have found the file
So if i in openfiledialog select a file in c:\tut\myfile.doc
then I write in a txt fil musik.txt in the folder c:\tut
And what i want is to write in the same file i read from /bin/debug/musik.txt
ALK
If you want to be SURE that you're always looking to the same file to read from and then write to, create a variable that holds that and always reference that variable in the rest of your methods. For example: Dim fileToProcess as String = ....
I'm not following why you're using a OpenFileDialog if you know in advance what file you want to work with. Maybe explain this a bit more?
After that, there are a number of ways to go about reading/writing to the file, but I think your issue right now is using the file that you want. Try setting a variable like I showed above, then reference that and I think you'll be on the right track.
Good luck!
:)- Hi
I shall use the path from openfiledialog, so i can read the file in my project
So a user (me) shall select a file, this file path a want to write in a text file
then later in the program i want to read from the txt fil and see the path to the file
so I can use the file. In this case the file is a MP3 file.
But i have to use the path, sp thats whay i use openfildialog.
So instead write in my code "musik.txt cant i write something whit apllicationpath ?
I have try believe me i have try, but it don't work
ALK I tried to show that in my first post. The problem with your WriteAllText is that you have not put the path and the file name together, and you passed the path as the second parameter...that is incorrect. Study this a bit, and see if it is any more clear to you.
The FileInfo class will give you the full path and name of your file, and WriteAllText will write to such a path.Dim fi As New FileInfo("musik.txt") Dim fileandpath As String = fi.FullName '' Array to hold strings from the file Dim alltext() As String ' This is an easier way to get all of the strings in the file. alltext = File.ReadAllLines(fileandpath) ' This will append the string to the end of the file. My.Computer.FileSystem.WriteAllText(fileandpath, _ "This is a literal string, or a string variable." & vbCrLf, _ True)I tried to show that in my first post. The problem with your WriteAllText is that you have not put the path and the file name together, and you passed the path as the second parameter...that is incorrect. Study this a bit, and see if it is any more clear to you.
The FileInfo class will give you the full path and name of your file, and WriteAllText will write to such a path.Dim fi As New FileInfo("musik.txt") Dim fileandpath As String = fi.FullName '' Array to hold strings from the file Dim alltext() As String ' This is an easier way to get all of the strings in the file. alltext = File.ReadAllLines(fileandpath) ' This will append the string to the end of the file. My.Computer.FileSystem.WriteAllText(fileandpath, _ "This is a literal string, or a string variable." & vbCrLf, _ True)
@Alvin ->
The above should take care of it. Add in what you need in order to use your OpenFileDialog and you should have it I would think?- I'm not in front of a PC at the moment.
If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
Mark the best replies as answers. "Fooling computers since 1971." I'm not in front of a PC at the moment.
If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
Mark the best replies as answers. "Fooling computers since 1971."
Rudy,
Good point.
@Alvin ->
Try something like this:
Dim baseFolder As String = Application.StartupPath Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Button to open the text file OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog1.FilterIndex = 1 OpenFileDialog1.FileName = baseFolder & "\*.txt" If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ' ----------------------- ' Start the rest here ... ' ----------------------- End If End Sub
- Marked As Answer byAlvin Kuiper Friday, November 06, 2009 9:19 AM
- Thank you
now its working.
And Janzai I'm sorry i could not get you example to work with openfiledialog
so i have to write again.
Alvin
ALK


