-
November 28th, 2004, 09:53 PM
#1
Make new file with OpenFileDialog
could someone give me a some sort of clue about opening nonexistant files in VB.NET??
I am learning this as I go, and I can open existing files using the OpenFileDialog box, but I cant not find an real explanation on how to create a file if it is not in existance...
in other words, I want to know now to code so that when a user enteres a filename in the filename box in the OpenFileDialog and that file does not already exist then the file is created as a new file, and then opened.
I have done some googleing and found all sorts of examples on opening existing files, but nothing that really gives a good explanation of how to create a new file and then open it...
Cheers
Jeff
-
November 29th, 2004, 09:02 AM
#2
Re: Make new file with OpenFileDialog
One way:
Dim fn as Integer
fn = Freefile
fielopen(fn,"C:\NewNonexisitngFile.dat",openmode.output)
To write to file:
print(fn, "ABCD")
To close the file after you write to it:
fileclose(fn)
Good luck
-
November 29th, 2004, 09:32 AM
#3
Re: Make new file with OpenFileDialog
Thanks, but I can do it in the background with FileOpen already... but how do you tie that in to the data in teh OpenFileDialog? for example, if I call the dialog like so:
openfiledialog.showdialog()
in a function, the user gets the standard windows dialog. Now, the filename that user inputs is assigned to the openfiledialog.filename property, IIRC, But can I pull that out with the click of the OK button?
Actually, I think I am misunderstanding something here... there is an OpenFIleDialog.FileOK event... is that a click event on the OK button in the OFD??? In otherwords, if I have an event for OpenFileDialog1.FileOK, will that trigger when the user clicks on the OK button, or is that an event that occurs if the OpenFileDialog checks for filename validity and existance, and then returns true on each??
Does that make sense... Now that i have had a nights sleep, that seems to make sense to me, so does it make sense to you?
IOW, would I do something like this:
private sub openfile_FileOK(sub declaration stuff here) handles OpenFileDialog.FileOK
Dim strFileName as String = OpenFileDialog.Filename, fn as Integer
Dim intButton as integer
If System.IO.File.Exists(strFileName) = False then
intButton = Messagebox.Show("File does not exist. Open new?", "header",messageboxbuttons.yesno)
if intButton = DialogResult.Yes then
fn = freefile
FileOpen(fn,strFileName,other stuff here)
Else
messagebox.show("File not opened","header",MessageBoxButtons.OK)
End If Else
fn = freefile
fileopen(fn,strFileName, other stuff here) End If
End Sub
to create a new file if the file does not exist??
-
November 29th, 2004, 10:10 AM
#4
Re: Make new file with OpenFileDialog
OpenFileDialog is meant to open an existing file, not a nonexisting file.
-
November 29th, 2004, 11:06 AM
#5
Re: Make new file with OpenFileDialog
Thats kinda waht I thought, but I was hoping there was a way around that... it is, after all, just a dialog...
In that case, how could I get that sort of dialog that does exactly what openfiledialog does, AND includes the ability to create new files if necessary? it seems a bit silly to me to have such a very well done predefined dialog, and just not include such a basic function like file creation. It will create new directories, after all...
Oh well... I can do it the long way by forcing the user to specifically create a new file via menu option, or open and existing one via openfiledialog... I was just hoping there was a way to do it inclusively...
Cheers
Jeff
-
November 29th, 2004, 11:49 AM
#6
Re: Make new file with OpenFileDialog
sounds to me like you wanna use a savefiledialog instead.
Nicolas Bohemier
-
November 29th, 2004, 12:00 PM
#7
Re: Make new file with OpenFileDialog
I dont know... IIRC, savefiledialog doesnt allow for opening of files.. only saving of files, yes?? granted that WOULD handle the creation part, but not the opening of the file after creation.
Here is a rundown of what I am trying to do:
1. dialog opens with standard file selection window and text boxes for entering filename and filtering by extension (just like the standard windows FileOpenDialog())
2. User enters file name if it is not seen in the provided file listing.
3. User clicks OK.
4. IF file exists, then pass that filename to a string var, and use that string var to open the file with fileopen()
5. IF the file does NOT exist, user gets a dialog saying "File does not exist, would you like to create it?"
5a. if user says yes, then create the file, then pass filename to string var and open with fileopen.
5b. if user says no, then cancle the operation, cancle the file open dialog and return to the main form.
6. close the dialog and return to main form for the rest of the program.
I guess I will have to make my own... I cant see any other way of doing it simply... other than haveing sepearate open and new file dialogs.. which is not out of the question.. I just wanted to add the ability to create new files to the openfile dialog in case the user wanted a new file that way...
Jeff
-
April 29th, 2010, 04:26 PM
#8
Re: Make new file with OpenFileDialog
I know this is an old post but I had this problem too and finally found a solution so I figured I would share with the world.
I used a solution very similar to Jeff's original idea but used:
.CheckFileExists = False
With the OFD to disable the built-in warning. Then you can do your own checks and if it doesn't exist just create it yourself.
Hope this helps some people.
Enjoy,
-Greeen
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
|