Welcome to Dream.In.Code
Become a VB Expert!

Join 137,234 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,640 people online right now. Registration is fast and FREE... Join Now!




Saving A File

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Saving A File

brottmayer
23 Jan, 2007 - 10:08 AM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
Hello,

I am trying to save a file that I create with the RichTextBox and when i go to save the file (and the file already exists) is doesn't save as a new file but instead adds to the existing file. So basically I end up with double the information, one with the original file (that's supposedly was suppose to be saved over) and then the original file with the added changes!

Here's the code:
CODE

Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter

        Save.Filter = "Plain Text File (*.ptf)|*.ptf|All Files(*.*)|*.*"
        Save.CheckPathExists = True
        Save.Title = "Save File"
        Save.ShowDialog(Me)

        Try
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
            myStreamWriter.Write(RichTextBox.Text)
            myStreamWriter.Close()
        Catch ex As Exception
            'Do Nothing
        End Try
    End Sub


Basically, I want to be able to save over a file and not have it double the information. Any help would be great. Thanks.
User is offlineProfile CardPM
+Quote Post

the_hangman
RE: Saving A File
23 Jan, 2007 - 10:10 AM
Post #2

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
Before you write the information into the file, write the file over as blank
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Saving A File
23 Jan, 2007 - 10:15 AM
Post #3

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(the_hangman @ 23 Jan, 2007 - 11:10 AM) *

Before you write the information into the file, write the file over as blank


Forgive me, but I am new to all this and learning on the fly. How would I write it out as a blank? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Saving A File
23 Jan, 2007 - 10:28 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You are currently using the AppendText method which does just that, appends to the end of the file or creates it if it does not exist.

Use the CreateText method to overwrite the file each time. It will wipe out anything in the file when it writes to it.

CODE

myStreamWriter = System.IO.File.CreateText(Save.FileName)

User is online!Profile CardPM
+Quote Post

the_hangman
RE: Saving A File
23 Jan, 2007 - 10:31 AM
Post #5

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
try

My.Computer.FileSystem.WriteAllText(Save.FileName, "", False)

NOTE: Posted too late.. Jayman's idea is better
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Saving A File
23 Jan, 2007 - 10:40 AM
Post #6

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 23 Jan, 2007 - 11:28 AM) *

You are currently using the AppendText method which does just that, appends to the end of the file or creates it if it does not exist.

Use the CreateText method to overwrite the file each time. It will wipe out anything in the file when it writes to it.

CODE

myStreamWriter = System.IO.File.CreateText(Save.FileName)



Jayman, Thanks for your help. It worked.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Saving A File
3 Feb, 2007 - 07:14 PM
Post #7

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(brottmayer @ 23 Jan, 2007 - 11:40 AM) *

QUOTE(jayman9 @ 23 Jan, 2007 - 11:28 AM) *

You are currently using the AppendText method which does just that, appends to the end of the file or creates it if it does not exist.

Use the CreateText method to overwrite the file each time. It will wipe out anything in the file when it writes to it.

CODE

myStreamWriter = System.IO.File.CreateText(Save.FileName)



Jayman, Thanks for your help. It worked.



How do I go about setting up the program to automatically save to that saved file? Forexample say I have File.rtf, how do I, when I hit the Ctrl+S, automatically save to that particular file, File.rtf? And also, how would I go about setting up a Save As... command? These two things are bugging the crap out of me, any help would be greatly appreciated. Thanks!!
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Saving A File
4 Feb, 2007 - 08:34 AM
Post #8

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE

How do I go about setting up the program to automatically save to that saved file? Forexample say I have File.rtf, how do I, when I hit the Ctrl+S, automatically save to that particular file, File.rtf? And also, how would I go about setting up a Save As... command? These two things are bugging the crap out of me, any help would be greatly appreciated. Thanks!!



If anybody can help me with this, I would greatly appreciate it. I've been tying for at least a week and a half and I am coming up blank. ohno.gif crazy.gif

Brandon
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Saving A File
4 Feb, 2007 - 10:24 AM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Are you using a menustrip on the top of your form?

If so, then set the shortcut for the Save menu item to Ctrl-S. You might want to create two seperate procedures one for the Save and a seperate on for the Save As.

To create a SaveAs dialog it is the same as the code you have for the Save dialog in your first post. Just change the title to say "Save As".
User is online!Profile CardPM
+Quote Post

brottmayer
RE: Saving A File
4 Feb, 2007 - 03:11 PM
Post #10

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 4 Feb, 2007 - 11:24 AM) *

Are you using a menustrip on the top of your form?

If so, then set the shortcut for the Save menu item to Ctrl-S. You might want to create two seperate procedures one for the Save and a seperate on for the Save As.

To create a SaveAs dialog it is the same as the code you have for the Save dialog in your first post. Just change the title to say "Save As".



Thanks for responding. I have set the shortcut for the two command, Save and Save As. I have set the same coding for the Save As as I did with the Save. But when I have saved a file that I am currently working on, it opens up the SaveDialog and asks to save it over again. Whereas I want to accomplish the task of it automatically saving it to the currently opened file. How would I go about doing that? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Saving A File
4 Feb, 2007 - 04:53 PM
Post #11

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Don't use the SaveFileDialog when you click Save, you only need it in the Save As event.

All you need to do is just save the file.

Code inside the Save click event:
CODE

        Dim myStreamWriter As System.IO.StreamWriter

        Try
            myStreamWriter = System.IO.File.CreateText(Save.FileName)
            myStreamWriter.Write(RichTextBox.Text)
            myStreamWriter.Close()
        Catch ex As Exception
            'Do Nothing
        End Try

User is online!Profile CardPM
+Quote Post

brottmayer
RE: Saving A File
4 Feb, 2007 - 05:32 PM
Post #12

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 4 Feb, 2007 - 05:53 PM) *

Don't use the SaveFileDialog when you click Save, you only need it in the Save As event.

All you need to do is just save the file.

Code inside the Save click event:
CODE

        Dim myStreamWriter As System.IO.StreamWriter

        Try
            myStreamWriter = System.IO.File.CreateText(Save.FileName)
            myStreamWriter.Write(RichTextBox.Text)
            myStreamWriter.Close()
        Catch ex As Exception
            'Do Nothing
        End Try



When I do that, it is not recognizing the save in "myStreamWriter = System.IO.File.CreateText(Save.FileName)", what should i do? Thanks.
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 04:01PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month