|
-
May 4th, 2012, 01:36 AM
#1
Thread Starter
Junior Member
FileStream Question
I am trying to find the best way to implement the concept of Save and Save As. I currently have a save option which will provide a save dialog. I want to be able to both save a new file and also save changes to an existing file. The problem that I have is that I have only one save function which will always display a save dialog. I realize that I can have another function to bypass the save dialog however I am looking to consolidate my code by adding a check to the existing save function. If x = false meaning that the file has not been saved before and so we can display a dialog save. Else, if x = true, we already have a file and we simply want to save the existing file:
Code:
if x = false
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
fileStream = CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
End If
Else
fileStream = existingFilePath
End If
How can I assign the file stream an existing file path?
Also, is there a better way to save an existing file without overwriting the existing file completely?
Last edited by mk48; May 4th, 2012 at 01:40 AM.
-
May 4th, 2012, 01:58 AM
#2
Re: FileStream Question
Follow the CodeBank link in my signature and check out my thread on Implementing Save & Save As. It's generally not practical to edit the contents of a file in place. You pretty much have to just write the new file over the top.
-
May 4th, 2012, 02:14 AM
#3
Thread Starter
Junior Member
Re: FileStream Question
Thanks. So you are using different functions to implement the save/save as functionality. This was the route I was going to take if my first option is not feasible. Back to my original question, can I assign a filestream an existing file path? I need to save an xml file. I am saving a lot of information and so It would me better to just use one save function instead of two.
-
May 4th, 2012, 02:25 AM
#4
Re: FileStream Question
The fact that you are saving a lot of information is neither here nor there as far as whether to use one method or two is concerned. The decision on whether to use one method or two should be based solely on the functionality to be provided. If you take a look at the actual implementation in my CodeBank thread you will see that the actual saving of the file occurs in one place only, i.e. the Save method, which is completely appropriate. The purpose of the SaveAs method is to allow the user select a file path to save to. If you call SaveAs it will select the file path and then it calls Save to actually do the saving. Likewise, if you call Save and there isn't already a file path selected to save to, it calls the SaveAs method to select a file path. In that case, you would call Save, it would call SaveAs and it would call Save again. Each method should do one thing and one thing only, calling other methods to do other things. Save does the saving and SaveAs selects the file path.
As for the FileStream, you specify the path of the file when you create it by passing a String to the constructor. You also specify at that point whether to read or write and whether to overwrite or append. Obviously if you can overwrite or append, you can specify an existing file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|