5,661,954 members and growing! (13,779 online)
Email Password   helpLost your password?
Development Lifecycle » Installation » General     Advanced License: The Code Project Open License (CPOL)

Shadow Copying of Applications

By Günther M. FOIDL

Shadow copied applications aren't locked by the loader, so they can be updated/substituted at runtime.
C#, .NET CF, .NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5, .NET, Win32, Win64, Dev

Posted: 6 Oct 2008
Updated: 16 Oct 2008
Views: 9,411
Bookmarked: 86 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
32 votes for this Article.
Popularity: 7.00 Rating: 4.65 out of 5
1 vote, 3.1%
1
1 vote, 3.1%
2
1 vote, 3.1%
3
4 votes, 12.5%
4
25 votes, 78.1%
5

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


Engineer in combustion engine development.
Programming languages: FORTRAN 95, C#, Matlab, Visual Basic 6
Occupation: Software Developer (Senior)
Company: Foidl Günther
Location: Austria Austria

Other popular Installation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralUser priviliges?memberTobiasP4:08 15 Oct '08  
GeneralRe: User priviliges?memberGünther M. FOIDL4:48 15 Oct '08  
GeneralRe: User priviliges?memberEd.Poore12:44 16 Oct '08  
GeneralRe: User priviliges?memberGünther M. FOIDL13:07 16 Oct '08  
GeneralRe: User priviliges?memberEd.Poore13:14 16 Oct '08  
GeneralEnvironment.CurrentDirectory is wrong...memberDanilo Corallo3:01 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL3:24 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...memberDanilo Corallo3:33 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL4:06 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...membernicorac21:38 13 Oct '08  
AnswerRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL22:43 13 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...membernicorac4:21 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... [modified]memberGünther M. FOIDL7:37 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...memberSkylinc19:52 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong...memberGünther M. FOIDL22:18 14 Oct '08  
GeneralOn deleting...memberpikipoki5:24 9 Oct '08  
GeneralRe: On deleting...memberGünther M. FOIDL5:44 9 Oct '08  
GeneralRe: On deleting...memberdejanstanic9:08 9 Oct '08  
GeneralMultiple instances and updating multiple filesmemberTobiasP1:26 18 Oct '08  
GeneralRe: Multiple instances and updating multiple filesmemberGünther M. FOIDL3:01 18 Oct '08  
GeneralNicememberCaio Kinzel Filho9:38 7 Oct '08  
GeneralRe: NicememberGünther M. FOIDL9:47 7 Oct '08  
GeneralRe: NicememberCaio Kinzel Filho10:12 7 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by Günther M. FOIDL
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project