none
File Open to Start program RRS feed

  • Question

  • Hello,

    I have built a winforms application on VS2010 (using VB).  I have a file-open option within the program to open up files with a particular file extension.

    When published, I have also assocated these files with the program, and in Windows I have used the open-with option, so that when I double click these files in the desktop environment, the winforms application will open.

    However, all it does is open the program with the default options, and not the data stored in the file itself, as would normally happen if the user chose the file-open option within the program.

    is it possible to use the files (in desktop/explorer), use them to open the program, and then get the program to open the file.

    Perhaps there is something in form_load() function that could do this? e .g. get file name or something?

    Thanks,

    Phil

    Tuesday, October 16, 2012 9:16 AM

Answers

  • Hi,

    in VB its a lot easier, you can get the Arguments like (you dont need to update the main method, if you use a StartupForm and have the application framework enabled as it usually is):

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            If Not My.Application.CommandLineArgs Is Nothing AndAlso My.Application.CommandLineArgs.Count > 0 AndAlso System.IO.File.Exists(My.Application.CommandLineArgs(0)) Then
                MsgBox(My.Application.CommandLineArgs(0)) '... and load file into GUI
            End If
        End Sub

    Regards,

      Thorsten




    • Marked as answer by newmanpj Tuesday, October 16, 2012 1:02 PM
    • Edited by Thorsten Gudera Tuesday, October 16, 2012 3:09 PM
    Tuesday, October 16, 2012 12:47 PM
  • Hi,

    something to check [maybe you're running a 64 bit win7 with a anycpu-compiled app?]:

    http://social.msdn.microsoft.com/Forums/en/winformssetup/thread/e4db9bfd-dd83-42b9-a3b6-7e4c0a7fe198

    Regards,

      Thorsten

    • Marked as answer by newmanpj Friday, November 16, 2012 3:13 PM
    Friday, November 16, 2012 3:03 PM

All replies

  • For info,

    the form_load function is like this

    Private Sub PolePlacer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
     ' set initial values of form
     ' Call function to get data, perform calculations
        End Sub

    If there is a way perhaps to determine whether the event sequence was a "clean" load, or a file-based load of the form.

    however, I'm not an expert here, so I'm not entirely sure if this is possible, and if so, how it would be done!

    Thanks

    Tuesday, October 16, 2012 9:53 AM
  • HelloPhil.
    I may havemisunderstood.Butsome optionsyou useto open the program,right?
    Theseoptions can be usedinapp.config,being addedin the project properties.
    It can also bereceived asparametersthe optionyou want to use.
    At themoment:

    http://msdn.microsoft.com/en-us/library/bc6ws923(v=vs.100).aspx


    http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/2f3df251-1395-4143-953e-41c5f710d720/


    If was useful mark as answered. Mariano, Paulo T. C.

    Tuesday, October 16, 2012 11:29 AM
  • Hi,

    I'll try to clarify.  If you open the application the usual way the program will load with a set of default values, coded in the application.

    however, I want to open the application by using an associated file, which will the load the form and then load the data stored in the file, e.g. different data from the default.

    For example, if you open a *.pdf file by clicking on the file (say in explorer), not only does it open Adobe Acrobat, but it also loads the file you've just clicked on.

    I want to do this with my file and application.  At the moment, all it does is just open the application with the default values.

    What I guess needs to happen, is for the application to self-determine whether the application has been loaded in the "usual way" (start menu, or shortcut or whatever) or by double-clicking an associated file.

    I've already got code to read the data from the file, I just need to determine how the application was opened.

    Thanks

    Tuesday, October 16, 2012 11:45 AM
  • Hi,

    when you doubleclick a file thats associated with a program, the program will be startet with at least one argument - the path to the file. So in your program you need to handle this. If some commandlineargs are present, do something like for instance load the file...

    Amodified Main method in program.cs:

            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                if (args.Length > 0)
                    Application.Run(new Form1(args));
                else
                    Application.Run(new Form1());
            }

    A second constructor in Form1:

        public partial class Form1 : Form
        {
            private string[] args;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            public Form1(string[] args)
            {
                this.args = args;
    
                if (args.Length > 0 && System.IO.File.Exists(args[0]))
                {
                    //load the file into the GUI
                }
            }
        }

    Regards,

      Thorsten



    Tuesday, October 16, 2012 12:12 PM
  • Thanks, this looks useful.

    Firstly, I am using VB, so I'll have to try and translate your code, but thank you for it.

    Secondly, I can't seem to change the startup bit from a form (eg Form1) to a Main (either Module, or something else?).

    Phil

    Tuesday, October 16, 2012 12:29 PM
  • Hi,

    in VB its a lot easier, you can get the Arguments like (you dont need to update the main method, if you use a StartupForm and have the application framework enabled as it usually is):

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            If Not My.Application.CommandLineArgs Is Nothing AndAlso My.Application.CommandLineArgs.Count > 0 AndAlso System.IO.File.Exists(My.Application.CommandLineArgs(0)) Then
                MsgBox(My.Application.CommandLineArgs(0)) '... and load file into GUI
            End If
        End Sub

    Regards,

      Thorsten




    • Marked as answer by newmanpj Tuesday, October 16, 2012 1:02 PM
    • Edited by Thorsten Gudera Tuesday, October 16, 2012 3:09 PM
    Tuesday, October 16, 2012 12:47 PM
  • My.Application.CommandLineArgs(0) is the first argument passed to the exe file. Usually the path to the file that should be opened by the application.

    Regards,

      Thorsten

    Tuesday, October 16, 2012 2:07 PM
  • Thanks, that's brilliant.

    Somehow, Windows (7) has stopped the file associations, and my *.ppf files now only open in the default program (notepad).  there was no file extention of this type before.  I can't seem to change the program to open it.

    I have managed to get the file to open by dragging it to the exe file to open it, which shows that the code above works.

    But frustratingly, I can't get the file assocation working.  I swear it was working before.  Perhaps it's because I didn't uninstall the application properly - just deleted the files (that's because I don't have the admin rights to uninstall programs).

    I doubt it's VS related, more likely a windows problem, though if anyone has any good ideas on how to resolve it, that would be good!

    Thanks,

    Phil

    Tuesday, October 16, 2012 2:09 PM
  • Hi,

    rightclick the file, choose "open with" and then the last item in the menu (something like "choose standardprogram")...

    Regards,

      Thorsten

    Tuesday, October 16, 2012 2:10 PM
  • Thanks.  That's how I got it to work initially.  Since then, I've removed the application files and re-published, and it's not worked since.  When I go to 'browse' and select the application exe file, it doesn't get added to the list.

    Other programs get added to the list (e.g. wordpad, notepad) but not mine.

    Phil

    Tuesday, October 16, 2012 2:15 PM
  • Hm, hard to say from here...

    What's persisted in the registry?

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.YourFileExtension\OPenWithList and maybe UserChoice...

    [Note: BackUp each registry key and values before editing]

    Regards,

      Thorsten

    Tuesday, October 16, 2012 2:32 PM
  • Unfortunately, I don't have access to the registry (it's a work machine).  I'll have to kindly ask the IT department to have a look.

    Thanks for all your help.

    Philip

    Tuesday, October 16, 2012 2:37 PM
  • Hello again,

    In response to your suggested code answer, I have managed to find a suitable Windows 7 development machine, so I can now test this function.

    Unfortunately, it doesn't appear to work.  When double clicking the file, it loads the program, but it doesn't open the file and load the data, so I end up with just the default data.   That is, it seems like the first part of the 'if' statement hasn't worked, and it skips straight to the 'else' section.

    here is my code.

    Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' determine how the application is opened, whether through the main icon, or through an associated file
            If Not My.Application.CommandLineArgs Is Nothing AndAlso My.Application.CommandLineArgs.Count > 0 AndAlso System.IO.File.Exists(My.Application.CommandLineArgs(0)) Then
                filename2 = My.Application.CommandLineArgs(0)
                openfile(filename2)
            Else
                ' set initial values of form
                GetData()
            End If
        End Sub

    Any ideas on why this might not be functioning will be appreciated.  It's unforunate I cannot test this in 'debug' mode.

    Philip

    Friday, November 16, 2012 12:43 PM
  • By the looks of things, My.Applicatoin.CommandLineArgs has nothing in it.  I used this code, and the msgbox was empty.  that would explain why the above solution wasn't working.

    Question is, a) why is the commandline empty, b) should it be empty? c) if commandline won't work, what will?

    ' Displays a message box that shows the full command line for the 
    ' application. 
    Dim args As String = "" 
    For Each arg As String In My.Application.CommandLineArgs
        args &= arg & " " 
    Next
    MsgBox(args)

    Friday, November 16, 2012 1:26 PM
  • Hi,

    check what part(s) of the if statement are false so it steps into the else block by putting either a breakpoint to the if-statement and hovering the mouse over it or by removing the parts of the if-statement one by one to see when it works as it should...

    So as an example change the if statement in a first test to:

            If Not My.Application.CommandLineArgs Is Nothing AndAlso My.Application.CommandLineArgs.Count > 0 Then

    and see, if it steps in now [be aware of other errors occuring then, if you for instance have no access rights to the file passed as argument.]

    Regards,

      Thorsten

    Friday, November 16, 2012 1:27 PM
  • Question is, a) why is the commandline empty, b) should it be empty?

    Hi, this is something you should know... :-)

    Do you open the program by doubleclicking an associated file, or dropping a file to the exe? If so, the commandlineargs shouldnt be empty.

    Regards,

      Thorsten

    Friday, November 16, 2012 1:31 PM
  • I would like to open the program in two was:

    double clicking the exe file, which will load default data

    double clicking an associated file, which will then load the program and open the file.

    Unfortunately, I've only been using vb.net for a few months, so I'm not overly familiar with it.

    I'm assuming that to open the file, I would need to 'drag and drop' it on the exe file?

    Friday, November 16, 2012 1:42 PM
  • double clicking an associated file should:

    -start the program

    -pass the file-path as commandlineargument to the program

    so, if doubleclicking the file opens the program, the file association is correct - and the commandlineargs shouldnt be empty...

    Regards,

      Thorsten

    Friday, November 16, 2012 1:51 PM
  • well, the program loads, but the commandlineargs are empty.  the *.ppf files that are associated have the same icon as the program, and load the program. it seems as though there is no information in the commandlineargs.

    FYI, it works fine when the file is 'drag and dropped' to the exe file, but that's long winded way of doing it!

    Does there need to be any code or suchlike in my application events?

    Friday, November 16, 2012 1:58 PM
  • I should also add, and having read a little more feel that it might be important (!) is that this is a clickonce deployment application.

    Friday, November 16, 2012 2:16 PM
  • If so, everything with the program is ok, because dropping it to the exe is nothing else than doubleclicking the file - if the filetype and the program are associated. So the problem must be somewhere in the chain from doubleclicking to passing it to the program...

    I never had this issue so I can only suggest to setup a simple small testproject, add the code to loop the args with the msgbox to the form load event and do a test first with specifying a commandline arg in the project properties...:

    The class then should look like:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim args As String = ""
            For Each arg As String In My.Application.CommandLineArgs
                args &= arg & " "
            Next
            MsgBox(args)
        End Sub
    End Class

    step2:

    [click "project"->"yourprojName Properties" click the debug-tab and simply write some text to the commandline arguments textbox. Then run the testprogram and see, if the args are passed correctly. After this is veryfied, associate a testfiletype to the program and test again by now doubleclicking that test-file [put the file into a location the program has read acces to].]

    Regards,

      Thorsten



    Friday, November 16, 2012 2:17 PM
  • I should also add, and having read a little more feel that it might be important (!) is that this is a clickonce deployment application.

    OKay, do you have the filetype associated like:

    http://msdn.microsoft.com/en-us/library/bb892924.aspx

    ?

    Regards,

      Thorsten

    Friday, November 16, 2012 2:21 PM
  • Hi,

    I believe so - the file association was created in Visual Studio.  When I looked at the manifest file (in notepad) the file association info was all there.

    The only thing that hasn't been done is to "re-sign" the manifest, but since I've not added any further information, there doesn't seem a need to.  Also, I'm not sure how to, despite the "help" file!

    Philip

    Friday, November 16, 2012 2:42 PM
  • I got as far as halfway through step 2 - the command line argments are passed fine.  I didn't associate a testfiletype however.  still not sure how the program can run in debug mode when using an external file.  maybe this isn't the issue however.
    Friday, November 16, 2012 2:47 PM
  • Hi,

    if you show a messagebox with the cmdlineargs it doesnt matter, the box will be shown from any "mode". The only things ignored then are the args specified like the above in VS.

    Regards,

      Thorsten


    Friday, November 16, 2012 2:52 PM
  • it seems to be that "commandlineargs" only work with a clickonce application when it is run from a URL.  This isn't much use to me - so perhaps, and unfortunately, I cannot get this to work, unless I switch to a MSI type deployment, which I'd rather not do.
    Friday, November 16, 2012 2:54 PM
  • Hi,

    something to check [maybe you're running a 64 bit win7 with a anycpu-compiled app?]:

    http://social.msdn.microsoft.com/Forums/en/winformssetup/thread/e4db9bfd-dd83-42b9-a3b6-7e4c0a7fe198

    Regards,

      Thorsten

    • Marked as answer by newmanpj Friday, November 16, 2012 3:13 PM
    Friday, November 16, 2012 3:03 PM
  • BOOM! We have a winner!!

    I am running Windows 7 64 bit, and I tried the "open-with" and it works!!

    how frustratingly simple...!

    Thanks for all your time and help , it is much appreciated.

    Philip

    Friday, November 16, 2012 3:13 PM