Click here to Skip to main content
Click here to Skip to main content

Shadow Copying of Applications

By , 16 Oct 2008
 

Introduction

Running an application shadow copied can be useful for purposes like auto-updating. On normal execution, the assembly gets locked by the loader and can't be substituted while it's executed. On shadow copying, all assemblies referenced are copied to a cache path, and loaded/executed from this location - so the assemblies aren't locked, and can be changed.

Background

This technique is well known in ASP.NET, but on the application side, there's little information (even on MSDN). Therefore, I'd like to share my findings.

Using the Code

For executing an assembly from the cache path, we have to use a loader/bootstrapper. This little program creates a domain from which the application gets loaded. That means, when we want to start the application, we have to start the loader that loads the application for us.

The code for the application and all the referenced assemblies need no change.

The code for the loader is:

using System;
using System.IO;

namespace Loader
{
    static class Program
    {
        [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
        [STAThread]
        static void Main()
        {
            /* Enable shadow copying */

            // Get the startup path. Both assemblies (Loader and
            // MyApplication) reside in the same directory:
            string startupPath = Path.GetDirectoryName(
				System.Reflection.Assembly
				.GetExecutingAssembly().Location);

            // cache path = directory where the assemblies get
            // (shadow) copied:
            string cachePath = Path.Combine(
                startupPath,
                "__cache");
            string configFile = Path.Combine(
                startupPath,
                "MyApplication.exe.config");
            string assembly = Path.Combine(
                startupPath,
                "MyApplication.exe");

            // Create the setup for the new domain:
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "MyApplication";
            setup.ShadowCopyFiles = "true"; // note: it isn't a bool
            setup.CachePath = cachePath;
            setup.ConfigurationFile = configFile;

            // Create the application domain. The evidence of this
            // running assembly is used for the new domain:
            AppDomain domain = AppDomain.CreateDomain(
                "MyApplication",
                AppDomain.CurrentDomain.Evidence,
                setup);

            // Start MyApplication by executing the assembly:
            domain.ExecuteAssembly(assembly);

            // After the MyApplication has finished clean up:
            AppDomain.Unload(domain);
            Directory.Delete(cachePath, true);
        }
    }
}

Points of Interest

This simple program gives us the possibility to easily create auto updates (that replace the application's assembly).

History

  • 07 October 2008 - Initial release
  • 14 October 2008 - Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Günther M. FOIDL
Software Developer (Senior) Foidl Günther
Austria Austria
Member
Engineer in combustion engine development.
Programming languages: C#, FORTRAN 95, Matlab
 
FIS-overall worldcup winner in Speedski (Downhill) 2008/09 and 2009/10.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberRahul Rajat Singh26 Nov '12 - 20:13 
QuestionRequires Admin PermissionsmemberJeff Bowman18 Jun '12 - 13:29 
GeneralMy vote of 5membermanoj kumar choubey26 Feb '12 - 21:33 
QuestionUser priviliges?memberTobiasP15 Oct '08 - 3:08 
AnswerRe: User priviliges?memberGünther M. FOIDL15 Oct '08 - 3:48 
GeneralRe: User priviliges?memberEd.Poore16 Oct '08 - 11:44 
GeneralRe: User priviliges?memberGünther M. FOIDL16 Oct '08 - 12:07 
GeneralRe: User priviliges?memberEd.Poore16 Oct '08 - 12:14 
GeneralEnvironment.CurrentDirectory is wrong...memberDanilo Corallo10 Oct '08 - 2:01 
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL10 Oct '08 - 2:24 
GeneralRe: Environment.CurrentDirectory is wrong...memberDanilo Corallo10 Oct '08 - 2:33 
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL10 Oct '08 - 3:06 
GeneralRe: Environment.CurrentDirectory is wrong...membernicorac13 Oct '08 - 20:38 
AnswerRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL13 Oct '08 - 21:43 
GeneralRe: Environment.CurrentDirectory is wrong...membernicorac14 Oct '08 - 3:21 
Günther M. FOIDL wrote:
System.Windows.Forms.Application.StartupPath returns also the correct path.

 
Yeah, but works only in WinForms applications and not - for example - in console apps or services;
unless you add System.Windows.Forms.dll to references... Frown | :(
 

Visit my website for some interesting .NET free tools: http://coolsoft.altervista.org

GeneralRe: Environment.CurrentDirectory is wrong... [modified]memberGünther M. FOIDL14 Oct '08 - 6:37 
GeneralRe: Environment.CurrentDirectory is wrong...memberSkylinc14 Oct '08 - 18:52 
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL14 Oct '08 - 21:18 
GeneralOn deleting...memberpikipoki9 Oct '08 - 4:24 
GeneralRe: On deleting...memberGünther M. FOIDL9 Oct '08 - 4:44 
GeneralRe: On deleting...memberdejanstanic9 Oct '08 - 8:08 
GeneralMultiple instances and updating multiple filesmemberTobiasP18 Oct '08 - 0:26 
GeneralRe: Multiple instances and updating multiple filesmemberGünther M. FOIDL18 Oct '08 - 2:01 
GeneralNicememberCaio Kinzel Filho7 Oct '08 - 8:38 
GeneralRe: NicememberGünther M. FOIDL7 Oct '08 - 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 16 Oct 2008
Article Copyright 2008 by Günther M. FOIDL
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid