Jump to content

VB.NET - Streamwriter/Streamreader help

Go to solution Solved by WanderingFool,

I had actually forgot to turn the timer on... Thanks! But it didn't help. So I used the new code you provided which worked but does make the .txt file all messed up. Is there any way to replace all the text in that .txt file so when the new text from the textbox comes will not just be added onto what's already there. Because when it's imported later on it will be a lot of duplicated text in the file. Is this possible?

Oh yea, sorry I added in the append not really thinking :P

 

Using sw As StreamWriter = New StreamWriter("D:\testname.txt", true)

to

Using sw As StreamWriter = New StreamWriter("D:\testname.txt")

 

should do the trick.

 

As a note, if you ever get stuck with how to use something looking at C# examples can actually help.  While the syntax might be different, the function calls are pretty much the same (with that said VB examples are usually on MSDN anyways)

Hi!

 

So I want to use the streamwriter and reader without using the savefiledialog and openfiledialog. Basically what I have is a texteditor that is suppose to save a file without the user having to do anything(therefore I don't want the filedialogs). But basically there will be a timer running every 5 seconds and if there is a change in the textbox then it will write this change to a file that I will put in "C:\Program Files\Notes\autosave\FILENAME.txt" (FILENAME.txt will be the current date and time...) Then I want to be able to, if the program suddenly crashes or the user accidently shuts the program or computer off, turn the program on and then load the autosave. (With streamreader, without the openfiledialog)

 

So, the program is based around being able to restart and then just start the program again and you have your info you typed up before the reboot still there without a manual save. But I cannot remember how the streamwriter and reader works...

 

Remember that it's VB.NET, the language that requires the user to have net framwork. So no C# codes because the rest of the program is already done and I don't feel like redoing it all for C#...

 

 

Thanks!

 

Regards,

 

Ludwig Johnson

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well if I understand you right you could use something like this

 

Snippet of code from: http://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

        ' Get the directories currently on the C drive.         Dim cDirs As DirectoryInfo() = New DirectoryInfo("c:\").GetDirectories()        ' Write each directory name to a file.         Using sw As StreamWriter = New StreamWriter("CDriveDirs.txt")            For Each Dir As DirectoryInfo In cDirs                sw.WriteLine(Dir.Name)            Next         End Using         'Read and show each line from the file.         Dim line As String = ""         Using sr As StreamReader = New StreamReader("CDriveDirs.txt")            Do                line = sr.ReadLine()                Console.WriteLine(line)            Loop Until line Is Nothing         End Using 

The MSDN code is a good example of how to create the StreamWriter and reader with a constant file name.

 

If you want to append (which I think you do).  use New StreamWriter("location.txt", true)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Well if I understand you right you could use something like this

 

Snippet of code from: http://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

        ' Get the directories currently on the C drive.         Dim cDirs As DirectoryInfo() = New DirectoryInfo("c:\").GetDirectories()        ' Write each directory name to a file.         Using sw As StreamWriter = New StreamWriter("CDriveDirs.txt")            For Each Dir As DirectoryInfo In cDirs                sw.WriteLine(Dir.Name)            Next         End Using         'Read and show each line from the file.         Dim line As String = ""         Using sr As StreamReader = New StreamReader("CDriveDirs.txt")            Do                line = sr.ReadLine()                Console.WriteLine(line)            Loop Until line Is Nothing         End Using 

The MSDN code is a good example of how to create the StreamWriter and reader with a constant file name.

 

If you want to append (which I think you do).  use New StreamWriter("location.txt", true)

Whn I add this code to timer2_tick that has an interval of 1000ms then nothing happens:

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick        Dim cDirs As DirectoryInfo() = New DirectoryInfo("C:\").GetDirectories()        Using sw As StreamWriter = New StreamWriter("testname.txt")            For Each Dir As DirectoryInfo In cDirs                sw.WriteLine(Dir.Name)            Next        End Using    End Sub

I don't get any bugs but it doesn't create the .txt files when I debug the program either... The reason I changed the directory from: program files\notes\autosave    as I said I would have is because I thought the reason it didn't work might be because of lack of permissions and so on but when I changed it to just the c:\ it didn't make any difference so I guess it wasn't the problem.

 

EDIT: Also, I have imported system.IO so that isn't the problem. Though, if I wouldn't have done that I'm pretty sure I would have gotten errors.

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

This might be a dumb question, but I have been guilty of this so many times before so I need to ask....are you sure you started the timer?  Also if I might suggest put a breakpoint right on the sw.WriteLine to make sure it is being called (who knows maybe DirectoryInfo doesn't have permission to look for directories on C drive).

 

Or try this bit of code

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick        Using sw As StreamWriter = New StreamWriter("D:\testname.txt", true)            sw.WriteLine("test")        End Using    End Sub

That way it will eliminate the directory info and just test to see StreamWriter is working. (I find setting break points at the beginning and following the flow to see any errors can help a lot)....having the D:\ also prevents the any permission issues (if you don't have a D drive, just put it in a temp folder or somewhere you know will have permissions)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

This might be a dumb question, but I have been guilty of this so many times before so I need to ask....are you sure you started the timer?  Also if I might suggest put a breakpoint right on the sw.WriteLine to make sure it is being called (who knows maybe DirectoryInfo doesn't have permission to look for directories on C drive).

 

Or try this bit of code

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick        Using sw As StreamWriter = New StreamWriter("D:\testname.txt", true)            sw.WriteLine("test")        End Using    End Sub

That way it will eliminate the directory info and just test to see StreamWriter is working. (I find setting break points at the beginning and following the flow to see any errors can help a lot)....having the D:\ also prevents the any permission issues (if you don't have a D drive, just put it in a temp folder or somewhere you know will have permissions)

 

I had actually forgot to turn the timer on... Thanks! But it didn't help. So I used the new code you provided which worked but does make the .txt file all messed up. Is there any way to replace all the text in that .txt file so when the new text from the textbox comes will not just be added onto what's already there. Because when it's imported later on it will be a lot of duplicated text in the file. Is this possible?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

I had actually forgot to turn the timer on... Thanks! But it didn't help. So I used the new code you provided which worked but does make the .txt file all messed up. Is there any way to replace all the text in that .txt file so when the new text from the textbox comes will not just be added onto what's already there. Because when it's imported later on it will be a lot of duplicated text in the file. Is this possible?

Oh yea, sorry I added in the append not really thinking :P

 

Using sw As StreamWriter = New StreamWriter("D:\testname.txt", true)

to

Using sw As StreamWriter = New StreamWriter("D:\testname.txt")

 

should do the trick.

 

As a note, if you ever get stuck with how to use something looking at C# examples can actually help.  While the syntax might be different, the function calls are pretty much the same (with that said VB examples are usually on MSDN anyways)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Oh yea, sorry I added in the append not really thinking :P

 

Using sw As StreamWriter = New StreamWriter("D:\testname.txt", true)

to

Using sw As StreamWriter = New StreamWriter("D:\testname.txt")

 

should do the trick.

 

As a note, if you ever get stuck with how to use something looking at C# examples can actually help.  While the syntax might be different, the function calls are pretty much the same (with that said VB examples are usually on MSDN anyways)

Yes, yes it did indeed work now! Thank you SO much! I have been struggeling with this for a while now!

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×