CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Restart running application (C#, VS2010, Dotnet 2.0)

    Hi

    I have a program that is going to autoupdate itself if there is a new version on a ftp. After the update its supposed to restart itself, here is also the problem.

    I have read and tried lots of things but cant really get this to work, any ideas on how to do this would be much appreciated.

    Code:

    Code:
    private void Form1_Load(object sender, EventArgs e)
            {
                // Check if we have internet access
                if (IsConnected())
                {
                    // Check FTP for a new version
                    if (checkIfWeNeedAUpdate())
                    {
                        // Download new version to same folder as startup and named newVikingKundTerminal.exe
                        if (downloadNewVersion())
                        {
                            // Set new modified datetime so we wont download new version again
                            updateModifiedDatetime(remoteFile);
                            // Restart program
                            restartApplication();
                        }
                    }
                }
                else
    {
    // No internet, run in offline mode
    }
    
    
    private void restartApplication()
            {
                try
                {
                    if(File.Exists(Application.StartupPath + @"\oldVikingKundTerminal.exe"))
                    {
                        File.Delete(Application.StartupPath + @"\oldVikingKundTerminal.exe");
                    }
                    
                    File.Move(Application.StartupPath + @"\VikingKundTerminal.exe", Application.StartupPath + @"\oldVikingKundTerminal.exe");
                    
                    File.Move(Application.StartupPath + @"\newVikingKundTerminal.exe", Application.StartupPath + @"\VikingKundTerminal.exe");
                    
                    this.Close();
    
                    Process.Start(Application.ExecutablePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Omstart av programmet", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    Everything works until:
    Code:
    Process.Start(Application.ExecutablePath);
    The error says its trying to start oldVikingKundTerminal.exe....

    Suggestions/Ideas on how to accomplish this?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Restart running application (C#, VS2010, Dotnet 2.0)

    It looks like the currently running application knows it got renamed. Stepping through the code in a debugger can confirm this.

    At any rate try

    Code:
    Process.Start( Application.StartupPath + @"\VikingKundTerminal.exe );

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Restart running application (C#, VS2010, Dotnet 2.0)

    I had the same problem some time ago, and I used another exe to update the main application, actually they were updating each other, after the main app was updated I started it and it checked if the updater needs to be updated ;]
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Restart running application (C#, VS2010, Dotnet 2.0)

    Quote Originally Posted by memeloo View Post
    I had the same problem some time ago, and I used another exe to update the main application, actually they were updating each other, after the main app was updated I started it and it checked if the updater needs to be updated ;]
    That's probably the best way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured