[2005] Setting file defaults
ok,ya the title doesn't explain it too well. What I want to do though, is be able to set a default image for a file extention ( example: *.txt, *.ppt, *.ppf, etc..) as well as an application that opens it when you double-click one of them. And, also tell the app what to do when a user double-clicks one of the specific file and the app opens.
Example: I have made an app that saves a new file that has the extention of ".pgt". But, when it saves one, the file that has saved shows a random MS made image that means there is no image assigned to it. And, when I doulbe-click it, it gives me an error message that says Windows doesn't know what to do with this file. But I want it to open the application and read the file when I double click it.
I've seen this done with VC# before, but im not sure if its possible in VB. If it is could someone explain how it is done?:confused:
Re: [2005] Setting file defaults
To do that in Windows you open the Folder Options dialogue and edit the File Types tab.
To set it up for your app you have to create a Setup project and then use the File Types Editor to set up your file association. The necessary steps will be carried out by your installer automatically when your app is installed.
Now, when the user double-clicks a file in Windows Explorer it will run your app with the file path as a commandline argument. It's up to you to get that file path, test its legitimacy (because anyone could start your app with any commandline) and then process the file appropriately. You'd get the file path like so:
vb Code:
Dim cla As String() = Environment.GetCommandLineArgs()
'The first argument is always the path of the executable file itself.
If cla.Length > 1 Then
Dim filePath As String = cla(1)
If IO.File.Exists(filePath) Then
'The file exists so process it here.
'Note that there is no guarantee that the file contents are valid so take appropriate precautions.
End If
End If
Note that if you're using VB Express then Setup projects are not supported and ClickOnce publishing doesn't support file associations. If you're using VB Express then you'll either have to set up the file association manually or else use a third-party tool to create an installer.
Re: [2005] Setting file defaults
It CAN be done via code, but this is generally something that is done at install time.
You need to write keys to HKEY_CLASS_ROOT
I do this via INNO setup when I install my app. The packager that comes with Visual Studio offers this same functionality as well.
Re: [2005] Setting file defaults
I know how to make a file icon and default application through windows, but when I do that, it comes up with the icon being that piece of paper with the corner folded over, and the default application's icon being inside that paper only really small. I want it to be a whole new icon, but it won't let me do that.
Thats irrelevant though. You said that to set it up for my application, I have to make a setup application and my app has to be installed for that to work. But, I have downloaded apps before that had a menu toolbar item that set the file defaults when you clicked the button. What I am asking is how do I do something like that, where a user clicks a tool strip menu item to get that to set up.
Now, as for the double-click function, do I treat "If IO.File.Exists(filePath) Then" as I would "If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then" for an open file dialog for the same file type?
Re: [2005] Setting file defaults
Edit:
Ok, so I got it to do what I wanted when I double-click an icon, and it works just fine. Only problem now is that when I go to save it, it repeats the same action as what it did when I double-clicked the file. For me, I had it open an MDI child with text, but when I went to save new text, it saved it, then re-opened the same file with a new MDI child with the old text. It also occurrs when I open any new MDI child, or open anything from the menu tool strip. How the hell do I ge this to stop? (btw, I put the code above under the startup form's activate function: me.activiated. is that right, or do I put it somewhere else?)
Re: [2005] Setting file defaults
The Activated event is raised every time the form is activated. If the form is activated multiple times then your code will be executed multiple times, which is what you're seeing.
What you should so is put your code to open a file in its own method, which you can then call when and where it's needed. If you want to open a file when the user makes a selection from an OpenFileDialog then you call the method from there. If you want to open a file when the form loads then you call the method from the Load event handler, which is executed once for each form.