다음을 통해 공유


IAppHostConfigManager::GetConfigFile 메서드

요청된 구성 경로로 표시되는 구성 파일을 가져옵니다.

구문

HRESULT GetConfigFile(  
   [in,  
   string] BSTR bstrConfigPath,  
   [out,  
   retval] IAppHostConfigFile** ppConfigFile  
);  

매개 변수

bstrConfigPath
BSTR 요청되는 구성 파일 경로가 포함된 입니다.

ppConfigFile
IAppHostConfigFile 인터페이스의 포인터에 대한 포인터입니다.

반환 값

HRESULT입니다. 가능한 값에는 다음 표에 있는 값이 포함되지만, 이에 국한되는 것은 아닙니다.

설명
S_OK 작업이 성공했음을 나타냅니다.

설명

이 메서드는 구성 시스템 경로로 표시되는 물리적 구성 파일에 대한 액세스를 제공합니다. 예를 들어 매개 변수에 "MACHINE/WEBROOT/APPHOST"bstrConfigPath를 전달하면 ApplicationHost.config 파일을 나타내는 매개 변수의 ppConfigFile instance 반환 IAppHostConfigFile 됩니다.

예제

다음 코드 예제에서는 구성 파일을 가져오고, 새 섹션 그룹과 새 섹션을 추가하고, 새 섹션의 속성을 설정한 다음, 구성 시스템에 변경 내용을 커밋합니다.


#pragma once

#include <stdio.h>
#include <string.h>
#include <ahadmin.h>

int main()
{
    HRESULT                               hr          = S_OK;
    
    IAppHostWritableAdminManager        * pWMgr       = NULL;
    IAppHostConfigManager               * pCfgMgr     = NULL;
    IAppHostConfigFile                  * pCfgFile    = NULL; 
    IAppHostSectionGroup                * pRtSctnGrp  = NULL;
    IAppHostSectionGroup                * pSctnGrp    = NULL;
    IAppHostSectionDefinitionCollection * pSctnDefCol = NULL;
    IAppHostSectionDefinition           * pSctnDef    = NULL;

    BSTR bstrConfigCommitPath = SysAllocString(L"MACHINE/WEBROOT/APPHOST");
    BSTR bstrSctnGrpName      = SysAllocString(L"mySectionGroup");
    BSTR bstrSctnName         = SysAllocString(L"myNewSection");
    BSTR bstrDeny             = SysAllocString(L"Deny");
    BSTR bstrAppHostOnly      = SysAllocString(L"appHostOnly");

    // Initialize
    hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
    if ( FAILED( hr ) )
    {
        printf_s ( "ERROR: Unable to initialize\n" );
        goto exit;
    } 

    // Create
    hr = CoCreateInstance( __uuidof( AppHostWritableAdminManager ), NULL, 
            CLSCTX_INPROC_SERVER,
            __uuidof( IAppHostWritableAdminManager ), (void**) &pWMgr );
    if( FAILED( hr ) )
    {
        printf_s ( "ERROR: Unable to create an IAppHostWritableAdminManager\n" );
        goto exit;
    }

    pWMgr -> put_CommitPath ( bstrConfigCommitPath );

    // Get an IAppHostConfigManager
    hr = pWMgr -> get_ConfigManager ( &pCfgMgr );
    if ( FAILED( hr ) || ( &pCfgMgr == NULL ) )
    {
        printf_s ( "ERROR: Unable to get a config manager.\n" );
        goto exit;
    }
    
    // Get an IAppHostConfigFile
    hr = pCfgMgr -> GetConfigFile ( bstrConfigCommitPath, &pCfgFile );
    if ( FAILED ( hr ) || ( &pCfgFile == NULL ) )
    {
        if ( E_ACCESSDENIED == hr )
        {
            printf_s ( "ERROR: Access to configuration denied.\n" );
            printf_s ( "       Run sample as an administrator.\n" );
        }
        else
        {
            printf_s ( "ERROR: Unable to get config file.\n" );
        }
        goto exit;
    }

    // Get the root section group
    hr = pCfgFile -> get_RootSectionGroup ( &pRtSctnGrp );
    if ( FAILED ( hr ) || ( &pRtSctnGrp == NULL ) )
    {
        printf_s ( "ERROR: Unable to access root section group\n" );
        goto exit;
    }

    // Add a new section group
    hr = pRtSctnGrp -> AddSectionGroup ( bstrSctnGrpName, &pSctnGrp );
    if ( FAILED ( hr ) || ( &pSctnGrp == NULL ) )
    {
        printf_s ( "ERROR: Unable to add new section group\n" );
        goto exit;
    }

    // Get the section collection
    hr = pSctnGrp -> get_Sections ( &pSctnDefCol );
    if ( FAILED ( hr ) || ( &pSctnDefCol == NULL ) )
    {
        printf_s ( "ERROR: Unable to access section collection\n" );
        goto exit;
    }

    // Add the new section
    hr = pSctnDefCol -> AddSection ( bstrSctnName, &pSctnDef );
    if ( FAILED ( hr ) || ( &pSctnDef == NULL ) )
    {
        printf_s ( "ERROR: Unable to add new section\n" );
        goto exit;
    }

    // Set the section attributes
    pSctnDef -> put_OverrideModeDefault ( bstrDeny );
    pSctnDef -> put_AllowDefinition ( bstrAppHostOnly );

    // Commit the changes to the configuration system
    pWMgr->CommitChanges ( );

exit:
    // Exiting / Unwinding
    if ( pRtSctnGrp != NULL )
    {
        pRtSctnGrp->Release(); 
        pRtSctnGrp = NULL;
    }
    if ( pSctnGrp    != NULL )
    {
        pSctnGrp->Release(); 
        pSctnGrp = NULL;
    }
    if ( pSctnDefCol != NULL )
    {
        pSctnDefCol->Release(); 
        pSctnDefCol = NULL;
    }
    if ( pSctnDef != NULL )
    {
        pSctnDef->Release(); 
        pSctnDef = NULL;
    }
    if ( pCfgFile != NULL )
    {
        pCfgFile->Release();
        pCfgFile = NULL;
    }
    if ( pCfgMgr != NULL )
    {
        pCfgMgr->Release();
        pCfgMgr = NULL;
    }
    if ( pWMgr != NULL )
    {
        pWMgr->Release(); 
        pWMgr = NULL;
    }

    SysFreeString( bstrConfigCommitPath );
    SysFreeString( bstrSctnGrpName );
    SysFreeString( bstrSctnName );
    SysFreeString( bstrDeny );
    SysFreeString( bstrAppHostOnly );

    // Uninitialize
    CoUninitialize();

    return 0;
};

요구 사항

형식 Description
클라이언트 - Windows Vista의 IIS 7.0
- Windows 7의 IIS 7.5
- Windows 8의 IIS 8.0
- WINDOWS 10 IIS 10.0
서버 - Windows Server 2008의 IIS 7.0
- Windows Server 2008 R2의 IIS 7.5
- Windows Server 2012의 IIS 8.0
- Windows Server 2012 R2의 IIS 8.5
- WINDOWS SERVER 2016 IIS 10.0
제품 - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0
- IIS Express 7.5, IIS Express 8.0, IIS Express 10.0
헤더 Ahadmin.h

참고 항목

IAppHostConfigManager 인터페이스