110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.Collections.Specialized;
namespace MyInstallContextNamespace
{
[RunInstallerAttribute(true)]
class InstallContext_Example : Installer
{
public InstallContext myInstallContext;
public override void Install( IDictionary mySavedState )
{
StringDictionary myStringDictionary = myInstallContext.Parameters;
if( myStringDictionary.Count == 0 )
{
Console.WriteLine( "No parameters have been entered in the command line "
+"hence, the install will take place in the silent mode" );
}
else
{
// Check whether the "LogtoConsole" parameter has been set.
if( myInstallContext.IsParameterTrue( "LogtoConsole" ) == true )
{
// Display the message to the console and add it to the logfile.
myInstallContext.LogMessage( "The 'Install' method has been called" );
}
}
// The 'Install procedure should be added here.
}
public override void Uninstall( IDictionary mySavedState )
{
// The 'Uninstall' procedure should be added here.
}
public override void Rollback( IDictionary mySavedState )
{
if( myInstallContext.IsParameterTrue( "LogtoConsole" ) == true )
{
myInstallContext.LogMessage( "The 'Rollback' method has been called" );
}
// The 'Rollback' procedure should be added here.
}
public override void Commit( IDictionary mySavedState )
{
if( myInstallContext.IsParameterTrue( "LogtoConsole" ) == true )
{
myInstallContext.LogMessage( "The 'Commit' method has been called" );
}
// The 'Commit' procedure should be added here.
}
static void Main( string[] args )
{
InstallContext_Example myInstallObject = new InstallContext_Example();
IDictionary mySavedState = new Hashtable();
if( args.Length < 1 )
{
// There are no command line arguments, create an empty 'InstallContext'.
myInstallObject.myInstallContext = new InstallContext();
}
else if( ( args.Length == 1 ) && ( args[ 0 ] == "/?" ) )
{
// Display the 'Help' for this utility.
Console.WriteLine( "Specify the '/Logfile' and '/LogtoConsole' parameters" );
Console.WriteLine( "Example: " );
Console.WriteLine( "InstallContext_InstallContext.exe /LogFile=example.log"
+" /LogtoConsole=true" );
return;
}
else
{
// Create an InstallContext object with the given parameters.
String[] commandLine = new string[ args.Length ];
for( int i = 0; i < args.Length; i++ )
{
commandLine[ i ] = args[ i ];
}
myInstallObject.myInstallContext = new InstallContext( args[ 0 ], commandLine);
}
try
{
// Call the 'Install' method.
myInstallObject.Install( mySavedState );
// Call the 'Commit' method.
myInstallObject.Commit( mySavedState );
}
catch( Exception )
{
// Call the 'Rollback' method.
myInstallObject.Rollback( mySavedState );
}
}
}
}
假设解决方案里有一个dll(比如XXX.dll,类名为X):
内部有个参数:public static bool DEBUG = true / false;
dll 调用状况:
解决方案里所有的项目都引用过这一dll,换句话说,每个项目内部都有一些会根据X.DEBUG设置为true或false执行一些不同代码的区块。
安装项目:
解决方案里包含了一个安装项目,包含了解决方案内项目内产生的所有前台安装程序需要的exe和dll。
编译目录:
建立一系列目录用来分门别类,比如:
项目编译后输出路径统一为: C:\X\
并制作了两个批处理文件
A.BAT : 用于在 X.DEBUG = true 下编译出来的exe和dll 从 C:\X\ 目录下 Copy 到 C:\X\A,一次类推,还把部分的dll Copy 到了 C:\X\AA 等...。
B.BAT : 用于在 X.DEBUG = false 下编译出来的exe和dll 从 C:\X\ 目录下 Copy 到 C:\X\B,一次类推,还把部分的dll Copy 到了 C:\X\BB 等...。
现在是编译后手动选择两个批处理文件其中之一运行。
要求:
当XXX.dll内部X.DEBUG = true 后 编译解决方案,
自动运行A.BAT(等于是实现解决方案的“生成后事件”),而且安装项目自动打包并把安装程序输出到比如 C:\X\DebugSetup\Setup.msi
当XXX.dll内部X.DEBUG = false 后 编译解决方案,
自动运行B.BAT(等于是实现解决方案的“生成后事件”),而且安装项目自动打包并把安装程序输出到比如 C:\X\PublishedSetup\Setup.msi
也就是
1.自动按静态变量值拷贝编译出来的exe和dll到不同的目录,这些exe和dll用来更新服务端程序或上传IIS。
2.然后自动按静态变量值打包编译出来的exe和dll成安装程序msi到不同的目录,用来更新前台安装程序。