Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. executing a program by double clicking on a file shown in list view ( C# )

executing a program by double clicking on a file shown in list view ( C# )

Scheduled Pinned Locked Moved C#
helpcsharpquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gianpaolo Barci
    wrote on last edited by
    #1

    Hey people I´m still having a little problem here!!! I´m trying to execute a program by double clicking on a file shown in List View... The methods to show fthe files and folder I did and it´s working well... to execute a file I created a new mwthod executeFile

    private void executeFile(TreeNode nodeCurrent)
    {
    Process.Start(getFullPath(nodeCurrent.FullPath));
    Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
    }

    as you can see getFullPath(nodeCurrent.FullPath) gets the path for the directory where the file is. I don´t know if I did right but do call the method I greated another by doubleclickin on th list view lvFiles_SelectedIndexChanged and called the method executeFile(nodeCurrent) but It doesn´t work... it says nodeCurrent doesn´t exists on the namespace... I don´t know if I did executeFile right... Could some good soul please help me???? Thanks!

    S G 2 Replies Last reply
    0
    • G Gianpaolo Barci

      Hey people I´m still having a little problem here!!! I´m trying to execute a program by double clicking on a file shown in List View... The methods to show fthe files and folder I did and it´s working well... to execute a file I created a new mwthod executeFile

      private void executeFile(TreeNode nodeCurrent)
      {
      Process.Start(getFullPath(nodeCurrent.FullPath));
      Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
      }

      as you can see getFullPath(nodeCurrent.FullPath) gets the path for the directory where the file is. I don´t know if I did right but do call the method I greated another by doubleclickin on th list view lvFiles_SelectedIndexChanged and called the method executeFile(nodeCurrent) but It doesn´t work... it says nodeCurrent doesn´t exists on the namespace... I don´t know if I did executeFile right... Could some good soul please help me???? Thanks!

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi, don't know if I understand everything. Does the compiler say "Namespace doesn't exists" or does this error occur during runtime? Could you please post a bit more code, like the "lvFiles_SelectedIndexChanged" method? Sebastian

      G 2 Replies Last reply
      0
      • S SeMartens

        Hi, don't know if I understand everything. Does the compiler say "Namespace doesn't exists" or does this error occur during runtime? Could you please post a bit more code, like the "lvFiles_SelectedIndexChanged" method? Sebastian

        G Offline
        G Offline
        Gianpaolo Barci
        wrote on last edited by
        #3

        Hi Sebastian! The compiler says! Well the method just call execute file!!! Nothing more!

        private void executeFile(TreeNode nodeCurrent)
        {
        Process.Start(getFullPath(nodeCurrent.FullPath));
        Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
        }

        	private void lvFiles\_SelectedIndexChanged(object sender, System.EventArgs e)
        	{
        		executeFile(nodeCurrent);
        	}
        

        I tried to use shell dll too! But a little bit lost with it.... I know that I maybe doing something wrong but... I´m more familiar with structured C.

        S D 2 Replies Last reply
        0
        • S SeMartens

          Hi, don't know if I understand everything. Does the compiler say "Namespace doesn't exists" or does this error occur during runtime? Could you please post a bit more code, like the "lvFiles_SelectedIndexChanged" method? Sebastian

          G Offline
          G Offline
          Gianpaolo Barci
          wrote on last edited by
          #4

          Hi Sebastian I just tried another method that is ALMOST working!! Just a tiny detail that it open the program by doubleclicking the file BUT it doesn´t open the file itself!!!

          private void lvFiles_SelectedIndexChanged(object sender, System.EventArgs e)
          {
          ShellLib.ShellExecute shellExecute = new ShellLib.ShellExecute();
          shellExecute.Path = @"C:\Arquivos de programas\AutoCAD 2008\acad.exe";
          shellExecute.Execute();
          }

          just a simple shell dll command! Now I need to open the file itself!!! Any clue??? tnx!

          1 Reply Last reply
          0
          • G Gianpaolo Barci

            Hi Sebastian! The compiler says! Well the method just call execute file!!! Nothing more!

            private void executeFile(TreeNode nodeCurrent)
            {
            Process.Start(getFullPath(nodeCurrent.FullPath));
            Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
            }

            	private void lvFiles\_SelectedIndexChanged(object sender, System.EventArgs e)
            	{
            		executeFile(nodeCurrent);
            	}
            

            I tried to use shell dll too! But a little bit lost with it.... I know that I maybe doing something wrong but... I´m more familiar with structured C.

            S Offline
            S Offline
            SeMartens
            wrote on last edited by
            #5

            Well, where is nodeCurrent declared for lvFiles_SelectedIndexChanged? That's why the compiler says that there is no nodeCurrent... You have to get it the current selected item from your listview. Something like:

            // this is your selected item
            lvFiles.SelectedItems[0]

            By the way, are you using a TreeView or a ListView control? Regards Sebastian

            G 1 Reply Last reply
            0
            • G Gianpaolo Barci

              Hi Sebastian! The compiler says! Well the method just call execute file!!! Nothing more!

              private void executeFile(TreeNode nodeCurrent)
              {
              Process.Start(getFullPath(nodeCurrent.FullPath));
              Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
              }

              	private void lvFiles\_SelectedIndexChanged(object sender, System.EventArgs e)
              	{
              		executeFile(nodeCurrent);
              	}
              

              I tried to use shell dll too! But a little bit lost with it.... I know that I maybe doing something wrong but... I´m more familiar with structured C.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              First, it's not good practice to pass a TreeNode object to a method called "executeFile". It's kind of counter-intuitive. You should be passing in a string decribing the full path to a file. "executeFile" shouldn't need an entire TreeNode to do it's job. Now, what does getFullPath return when you pass in a TreeNode??

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              G 1 Reply Last reply
              0
              • S SeMartens

                Well, where is nodeCurrent declared for lvFiles_SelectedIndexChanged? That's why the compiler says that there is no nodeCurrent... You have to get it the current selected item from your listview. Something like:

                // this is your selected item
                lvFiles.SelectedItems[0]

                By the way, are you using a TreeView or a ListView control? Regards Sebastian

                G Offline
                G Offline
                Gianpaolo Barci
                wrote on last edited by
                #7

                It´s using both!!!! But treeView shows only folders ald list view only files!! I liked this way because it keeps wiewing more organized! I tried another method BUT I´m also trying this nodeCurrent is used to popuate lvFiles with files... it gets the current node and shows files. getFullPath gets the path from the root to the current node... I tried shell too you might see it because it almost worked.. just for the detail that it opns the program but not the file (I know that I´m missing a line or some statement) I´m lost because it´s my first c# experience!!!!

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  First, it's not good practice to pass a TreeNode object to a method called "executeFile". It's kind of counter-intuitive. You should be passing in a string decribing the full path to a file. "executeFile" shouldn't need an entire TreeNode to do it's job. Now, what does getFullPath return when you pass in a TreeNode??

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  G Offline
                  G Offline
                  Gianpaolo Barci
                  wrote on last edited by
                  #8

                  Hello Dave!!! getFullPath returns the path from the root to the current node! Yeah it makes sense to me now!!! It´s totally silly to pass something that is pointing to a directory to execute a file.... It doesn´t needs to know WHERE THE FIE IS because it is already showing me the files! I´m beginning to understand!

                  1 Reply Last reply
                  0
                  • G Gianpaolo Barci

                    Hey people I´m still having a little problem here!!! I´m trying to execute a program by double clicking on a file shown in List View... The methods to show fthe files and folder I did and it´s working well... to execute a file I created a new mwthod executeFile

                    private void executeFile(TreeNode nodeCurrent)
                    {
                    Process.Start(getFullPath(nodeCurrent.FullPath));
                    Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
                    }

                    as you can see getFullPath(nodeCurrent.FullPath) gets the path for the directory where the file is. I don´t know if I did right but do call the method I greated another by doubleclickin on th list view lvFiles_SelectedIndexChanged and called the method executeFile(nodeCurrent) but It doesn´t work... it says nodeCurrent doesn´t exists on the namespace... I don´t know if I did executeFile right... Could some good soul please help me???? Thanks!

                    G Offline
                    G Offline
                    Gianpaolo Barci
                    wrote on last edited by
                    #9

                    Hello folks! I´m almost reaching what I wanted to do! Now It does open the file... BUT JUST that file specified in the path on the code as you will see right down!

                    private void lvFiles_SelectedIndexChanged(object sender, System.EventArgs e)
                    {
                    ShellLib.ShellExecute shellExecute = new ShellLib.ShellExecute();
                    shellExecute.Verb = ShellLib.ShellExecute.OpenFile;
                    shellExecute.Path = @"F:\Painel de Controle ETECH\Projetos ETECH\Acessórios\80010005.dwg";
                    shellExecute.Execute();
                    }

                    It will open ONLY 80010005.dwg "Projetos ETECH" is the root node (folder) and contains a lot of childs (subfolders of especific projects) wich contains CAD drawings (thousands millions of DWGs). I need a way to get the path of any selected file. What I thought was to create a method that gets the path where you are and the name of the file you selected (string) and put them together to for the path to te file to be executed... Am I in the right way or there is something easier???? Best Regards for all you!!!!

                    S 1 Reply Last reply
                    0
                    • G Gianpaolo Barci

                      Hello folks! I´m almost reaching what I wanted to do! Now It does open the file... BUT JUST that file specified in the path on the code as you will see right down!

                      private void lvFiles_SelectedIndexChanged(object sender, System.EventArgs e)
                      {
                      ShellLib.ShellExecute shellExecute = new ShellLib.ShellExecute();
                      shellExecute.Verb = ShellLib.ShellExecute.OpenFile;
                      shellExecute.Path = @"F:\Painel de Controle ETECH\Projetos ETECH\Acessórios\80010005.dwg";
                      shellExecute.Execute();
                      }

                      It will open ONLY 80010005.dwg "Projetos ETECH" is the root node (folder) and contains a lot of childs (subfolders of especific projects) wich contains CAD drawings (thousands millions of DWGs). I need a way to get the path of any selected file. What I thought was to create a method that gets the path where you are and the name of the file you selected (string) and put them together to for the path to te file to be executed... Am I in the right way or there is something easier???? Best Regards for all you!!!!

                      S Offline
                      S Offline
                      SeMartens
                      wrote on last edited by
                      #10

                      Hi, it's me again. Sry, for didn't answering yesterday, was on my way home. Okay, so opening the file is not the problem. The problem is to get the path and name of the file to open. So let me guess that you doing something like this.

                      public string getFullPath(string sPath) {
                      string sFileName = lvFiles.SelectedItem[0].Text;
                      return System.IO.Path.Combine(sPath, sFileName);
                      }

                      private void executeFile(TreeNode nodeCurrent)
                      {
                      Process.Start(getFullPath(nodeCurrent.FullPath));
                      Process.Start(String.Format("\"{0}\"", getFullPath(nodeCurrent.FullPath)));
                      }

                      If you are doing this, you get an error from the compiler? Did you include the Namespace (using System.Windows.Forms)? Or rewrite it to

                      private void executeFile(System.Windows.Forms.TreeNode nodeCurrent) {
                      ...
                      }

                      Does this solve your problem? Regards Sebastian

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups