Assigning custom file extensions

vidflix

Member
Joined
Feb 7, 2009
Messages
5
Programming Experience
Beginner
Hi everyone,

this is my first post. ive been working on a project as a way of learning VB.NET 3.5.
the problem that im having is that im not sure how to assign a custom extension to my files that i create (".epcr"). so far, the registry recognizes my custom file type, i create the directory where i want the file saved, the file is named by the user.... but.... its saved as type "file" not type ".epcr"

heres some code:
----------------------------------------------------
System.IO.Directory.CreateDirectory("c:\EPCR")


Dim v_newFileName As String
v_newFileName = PtGivenNameTextBox.Text & PtSurnameTextBox.Text

SaveEpcrFileDialog.InitialDirectory = "C:\EPCR"
SaveEpcrFileDialog.Filter = "EPCR FILES | *.epcr"
SaveEpcrFileDialog.FileName = PtGivenNameTextBox.Text & PtSurnameTextBox.Text
SaveEpcrFileDialog.ShowDialog()
System.IO.File.Create("c:\EPCR\" & (PtGivenNameTextBox.Text & PtSurnameTextBox.Text))

End Sub

---------------------------------------

im still new to this and im teaching myself. any help would would be appreciated.
 
I figured it out, and it works! woo hoo!

Here's the code that works:

System.IO.Directory.CreateDirectory("c:\EPCR\")

Dim v_newFileName As String = PtGivenNameTextBox.Text & PtSurnameTextBox.Text
Dim v_newFileNameAndPath As String = "c:\EPCR\" & PtGivenNameTextBox.Text & PtSurnameTextBox.Text
Dim v_fileExtension As String = ".epcr"

SaveEpcrFileDialog.InitialDirectory = "C:\EPCR"
SaveEpcrFileDialog.Filter = "EPCR FILES | *.epcr"
SaveEpcrFileDialog.FileName = v_newFileName
SaveEpcrFileDialog.ShowDialog()
System.IO.File.Create(v_newFileNameAndPath & v_fileExtension)
 
Why are you using a SaveFileDialog here? You disregard both the filename user may input in dialog and the possible cancellation. Try this code:
VB.NET:
With savedialog
    .InitialDirectory = "c:\path"
    .Filter = "EPCR FILES | *.epcr"
    .FileName = "suggested name"
End With

If savedialog.ShowDialog = Windows.Forms.DialogResult.OK Then           
    MessageBox.Show("user says save to this file: " & savedialog.FileName)
Else
    MessageBox.Show("user doesn't want to save")
End If
Set the dialog properties in Designer if you don't need to change them in runtime.

Actually, if you don't need to do anything when user cancels you should just doubleclick the dialog in Designer and use the provided FileOK event handler where you fill in the code to preform in the "OK" case, in this case only this code:
VB.NET:
MessageBox.Show("user says save to this file: " & savedialog.FileName)
To call the dialog you would then only call savedialog.ShowDialog(), and the FileOK event is raised when user clicks the Save button.

Test the code some times with different input, notice how the FileName automatically includes the correct file extension.

Also one administative note; Do use formatting when posting, click the code button to put code in code box, etc.
 
Back
Top