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. C# method to restart the application

C# method to restart the application

Scheduled Pinned Locked Moved C#
helpcsharp
12 Posts 4 Posters 14 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.
  • L Lost User

    You can use following code to do so

    // Shut down the current app instance.
    Application.Exit();

    // Restart the app passing "/restart [processId]" as cmd line args
    Process.Start(Application.ExecutablePath, "/restart" + Process.GetCurrentProcess().Id);

    HTH

    Jinal Desai - LIVE Experience is mother of sage....

    A Offline
    A Offline
    ayandelhi
    wrote on last edited by
    #3

    How will it work can u plz explain.. i mean after this Application.Exit(); how will Process.Start(...); will be executed???

    1 Reply Last reply
    0
    • L Lost User

      You can use following code to do so

      // Shut down the current app instance.
      Application.Exit();

      // Restart the app passing "/restart [processId]" as cmd line args
      Process.Start(Application.ExecutablePath, "/restart" + Process.GetCurrentProcess().Id);

      HTH

      Jinal Desai - LIVE Experience is mother of sage....

      A Offline
      A Offline
      ayandelhi
      wrote on last edited by
      #4

      Ok its working.... Thanks for this... But will it work on a Console Application too... I cudnt find this Application... How shud i integrate it in my code?? Its not exiting but ya its opening another instance... How shud i get it working...

      L 1 Reply Last reply
      0
      • A ayandelhi

        Ok its working.... Thanks for this... But will it work on a Console Application too... I cudnt find this Application... How shud i integrate it in my code?? Its not exiting but ya its opening another instance... How shud i get it working...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Try following code

        Close();
        System.Diagnostics.Process.Start(Application.ExecutablePath);

        or you can also do following Add using System.Windows.Forms; to the top and call

        Application.Restart();

        You may need to add the reference: "System.Windows.Forms.dll" to your project.

        Jinal Desai - LIVE Experience is mother of sage....

        A 1 Reply Last reply
        0
        • L Lost User

          Try following code

          Close();
          System.Diagnostics.Process.Start(Application.ExecutablePath);

          or you can also do following Add using System.Windows.Forms; to the top and call

          Application.Restart();

          You may need to add the reference: "System.Windows.Forms.dll" to your project.

          Jinal Desai - LIVE Experience is mother of sage....

          A Offline
          A Offline
          ayandelhi
          wrote on last edited by
          #6

          Close() cant be used... m using Visual Studio 2010 n its not allowing... I mean its giving error... And Application.Restart() is working fine but it aint closing the previous instance... How shud i close the console window forcibly.. Should i use Process.Kill()??

          L 1 Reply Last reply
          0
          • A ayandelhi

            Close() cant be used... m using Visual Studio 2010 n its not allowing... I mean its giving error... And Application.Restart() is working fine but it aint closing the previous instance... How shud i close the console window forcibly.. Should i use Process.Kill()??

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            Ya you can also go for kill method to kill your process...

            Jinal Desai - LIVE Experience is mother of sage....

            A 1 Reply Last reply
            0
            • L Lost User

              Ya you can also go for kill method to kill your process...

              Jinal Desai - LIVE Experience is mother of sage....

              A Offline
              A Offline
              ayandelhi
              wrote on last edited by
              #8

              To kill the application i've used Process.GetCurrentProcess().Kill(); To restart { Process.Start(Application.ExecutablePath, "/restart" + Process.GetCurrentProcess().Id); Process.GetCurrentProcess().Kill(); } Thanks for the help... If u can find anyother more appropriate way plz let me know... Ayan

              1 Reply Last reply
              0
              • A ayandelhi

                Hi!! I wanna code a method that can restart the application without any user involvment.. I mean when an error is encountered, the method can be used to restart the application.. Plz help me out...

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                Application.Restart();

                stops the current process and starts a new identical one. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  Application.Restart();

                  stops the current process and starts a new identical one. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  A Offline
                  A Offline
                  ayandelhi
                  wrote on last edited by
                  #10

                  This one wont work in console application... this will open a new instance of the application but cant close the current instance...

                  P 1 Reply Last reply
                  0
                  • A ayandelhi

                    This one wont work in console application... this will open a new instance of the application but cant close the current instance...

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #11

                    How was Luc supposed to know it was a Console application? You didn't volunteer this info up front.

                    I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    A 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      How was Luc supposed to know it was a Console application? You didn't volunteer this info up front.

                      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Onyx

                      A Offline
                      A Offline
                      ayandelhi
                      wrote on last edited by
                      #12

                      ok... my bad...

                      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