Click here to Skip to main content
6,822,613 members and growing! (23,156 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#, .NETCF, .NET1.0, .NET1.1, .NET2.0, .NET3.0, .NET3.5, Win32, Win64, Dev
Posted:6 Oct 2008
Updated:16 Oct 2008
Views:21,985
Bookmarked:94 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 7.03 Rating: 4.71 out of 5

1
1 vote, 3.2%
2
1 vote, 3.2%
3
4 votes, 12.9%
4
25 votes, 80.6%
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


Member
Engineer in combustion engine development.
Programming languages: C#, FORTRAN 95, Matlab

FIS-overall wordlcup winner in Speedski (Downhill) 2008/09.
Occupation: Software Developer (Senior)
Company: Foidl Günther
Location: Austria Austria

Other popular Installation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralUser priviliges? PinmemberTobiasP4:08 15 Oct '08  
GeneralRe: User priviliges? PinmemberGünther M. FOIDL4:48 15 Oct '08  
GeneralRe: User priviliges? PinmemberEd.Poore12:44 16 Oct '08  
GeneralRe: User priviliges? PinmemberGünther M. FOIDL13:07 16 Oct '08  
GeneralRe: User priviliges? PinmemberEd.Poore13:14 16 Oct '08  
GeneralEnvironment.CurrentDirectory is wrong... PinmemberDanilo Corallo3:01 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... PinmemberGünther M. FOIDL3:24 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... PinmemberDanilo Corallo3:33 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... PinmemberGünther M. FOIDL4:06 10 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... Pinmembernicorac21:38 13 Oct '08  
AnswerRe: Environment.CurrentDirectory is wrong... PinmemberGünther M. FOIDL22:43 13 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... Pinmembernicorac4:21 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... [modified] PinmemberGünther M. FOIDL7:37 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... PinmemberSkylinc19:52 14 Oct '08  
GeneralRe: Environment.CurrentDirectory is wrong... PinmemberGünther M. FOIDL22:18 14 Oct '08  
GeneralOn deleting... Pinmemberpikipoki5:24 9 Oct '08  
GeneralRe: On deleting... PinmemberGünther M. FOIDL5:44 9 Oct '08  
GeneralRe: On deleting... Pinmemberdejanstanic9:08 9 Oct '08  
GeneralMultiple instances and updating multiple files PinmemberTobiasP1:26 18 Oct '08  
GeneralRe: Multiple instances and updating multiple files PinmemberGünther M. FOIDL3:01 18 Oct '08  
GeneralNice PinmemberCaio Kinzel Filho9:38 7 Oct '08  
GeneralRe: Nice PinmemberGünther M. FOIDL9:47 7 Oct '08  
GeneralRe: Nice PinmemberCaio Kinzel Filho10:12 7 Oct '08  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

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-2010
Web09 | Advertise on the Code Project