Click here to Skip to main content
6,822,123 members and growing! (17,885 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Applications     Intermediate License: The Code Project Open License (CPOL)

Get Rid of EXE/DLL Locks by using ShadowCopyFiles Appropriately

By Michael Ulmann

Get rid of EXE/DLL locks by using ShadowCopyFiles appropriately with .NET 2.0 and newer
C#, .NET (.NET2.0, .NET3.0, .NET3.5)
Posted:21 Dec 2007
Updated:21 Dec 2007
Views:9,391
Bookmarked:12 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 2.58 Rating: 3.32 out of 5
1 vote, 16.7%
1

2
1 vote, 16.7%
3

4
4 votes, 66.7%
5

Introduction

Unfortunately, the .NET Framework does not support modifications on the existing AppDomains. Therefore a wrapper is necessary in case someone wants to configure AppDomain settings for an executable. This article gives a brief overview how such a wrapper might look like.

Background

With the release of .NET Framework 2.0, Microsoft introduced a new philosophy regarding the configuration of created AppDomains. The idea is that existing AppDomain configurations must not be altered anymore. Thus, if you use methods that affect the configuration (e.g. ShadowCopyFiles) of an existing AppDomain, Visual Studio shows a warning, informing that this method is marked obsolete.

So far so good, problems occur in case you want to change the configuration of the default AppDomain of an application. One might expect support from the app.config file. Unfortunately, the framework does not contain such settings yet.
That's where the wrapper described in this article comes in.

Using the Code

The wrapper class is a very small, independent console application. It hosts a custom AppDomain in which the configured application is executed with the given settings.

using System;
using System.Configuration;

namespace SmartSoft.ExecutableWrapper
{
    internal class ExecutableWrapper
    {
        private static void Main(string[] commandLineArguments)
        {
            AppDomainSetup executableDomainSetup;
            AppDomain executableDomain;

            // Create app domain setup
            executableDomainSetup = new AppDomainSetup();
            // Apply settings
            executableDomainSetup.ShadowCopyFiles = 
                ConfigurationManager.AppSettings["ShadowCopyFiles"];
            // Create new app domain
            executableDomain = AppDomain.CreateDomain
                ("", AppDomain.CurrentDomain.Evidence, executableDomainSetup);
            // Execute configured application in the newly created app domain
            executableDomain.ExecuteAssembly(ConfigurationManager.AppSettings
                ["ApplicationFullPath"], AppDomain.CurrentDomain.Evidence, 
                commandLineArguments);
        }
    }
}

The App.config file contains the following appSettings:

<appSettings>
    <add key="ShadowCopyFiles" value="true"/>
    <add key="ApplicationFullPath" value="C:\AnyPath\MyExecutable.exe"/>
</appSettings>

Points of Interest

As you might have recognized, command line arguments are directly passed through to the executed application. In some cases, this needs to be changed.

The example above covers only the ShadowCopyFiles property of the AppDomainSetup. However, this approach can be expanded with any property or method being part of the AppDomainSetup.

History

  • 22nd December, 2007 - Initial post

License

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

About the Author

Michael Ulmann


Member
MCAD, MCPD Web Developer
Visit my Space: http://ukmichael.spaces.live.com
www.helveticsolutions.com
Occupation: Software Developer (Senior)
Company: Helvetic Solutions
Location: Australia Australia

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

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: 21 Dec 2007
Editor: Deeksha Shenoy
Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project