|
-
Jan 1st, 2006, 05:08 PM
#1
Thread Starter
Member
{Resolved} File Association Arguments ?..?..?
I am using vb.net exprtess 2005...
I have the file type associated with my application, so when I double-click this file, it starts my application.
Now I am wondering how the next step works...How do I recieve this file path?
I know windows sends the file path to my application, but I don't know how to recieve it so I can open this file...Anyone know?
Thanks...
Last edited by Modderman; Jan 2nd, 2006 at 01:33 AM.
-
Jan 1st, 2006, 08:29 PM
#2
Re: File Association Arguments ?..?..?
Here's a sample of how I did it in an application of mine. Basically, you start up using sub main in a module (making sure you change your startup object to Sub Main instead of your form), which accepts an args() parameter, which should be the filename passed in by windows. You would then need to have your application handle the passed in variable. The way I did it was modify the form constructor of the first form I loaded, running a certain procedure to open the file when a string was passed into the form...
VB Code:
'module that includes the Sub Main (set project to startup on Sub Main)
Module modStartup
Public Sub Main(ByVal args() As String)
MessageBox.Show(args(0)) 'just shows you the passed in string
If args.Length < 1 Then 'if less than one, run the form as normal
Application.Run(New frmMain(""))
Else
'pass in the string to the form constructor
Application.Run(New frmMain(args(0)))
End If
End Sub
End Module
'then in the form you run (frmMain in this example), have a certain procedure setup
'to run something if the string is not "", as in the below code:
Private FilePathArg As String 'form level variable to hold the string passed in
'now in the windows generated code section of your form, changing the New() constructor to accept a string parameter..
Public Sub New(ByVal FilePath As String) 'modified constructor
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
FilePathArg = FilePath 'sets form level string variable to the passed in variable
'Add any initialization after the InitializeComponent() call
End Sub
'then in form load, handle the case when a string is passed in..
If FilePathArg <> "" Then 'if not "", then a string was passed in...
'run your code to open the file as you like, FilePathArg is the
'string containing the file path
End If
Note that this was used in 2003, so there might be something a little different in the Windows generated code section, but everything should work just about the same...
Last edited by gigemboy; Jan 1st, 2006 at 08:42 PM.
-
Jan 1st, 2006, 09:51 PM
#3
Re: File Association Arguments ?..?..?
Don't worry about passing arguments to your Main method. Just use the Environment.GetCommandLineArgs method:
VB Code:
Dim cla As String() = Environment.GetCommandLineArgs()
'The first command-line argument is always the executable itself.
If cla.Length > 1 Then
MessageBox.Show("File to open: " & cla(1))
End If
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
|