Udostępnij za pośrednictwem


Jak zdefiniować importera zawartości

Aby zdefiniować importera zawartości technologii wdrażania zarządzania aplikacjami, użyj wystąpienia Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter klasy . Nowe wystąpienie klasy zdefiniuje importera zawartości używanego przez instalatora.

Klasa ContentImporter zapewnia projekt na poziomie interfejsu, w którym technologie niestandardowe mogą tworzyć wystąpienia i wypełniać obiekty DeploymentType dla określonej technologii. Koncepcja dla tej klasy polega na tym, że dany importer technologii może odczytać określony plik zawartości i utworzyć odpowiedni obiekt DeploymentType (z instalatorem) przy użyciu informacji uzyskanych z zawartości. Na przykład technologia Instalator Windows odczytuje *.msi plików i może wypełnić właściwości Tytuł, Opis obiektu DeploymentType i utworzyć akcje Wykrywanie, Instalowanie i odinstalowywanie instalatora.

W przykładowym projekcie protokołu RDP (Remote Desktop Protocol) dla plików RDP (Remote Desktop Protocol) wymagany jest nowy importer zawartości. Obsługa importowania zawartości dla plików RDP nie jest wbudowana w Configuration Manager, dlatego jest wymagany niestandardowy importer zawartości.

Podstawowe omówienie definiowania niestandardowego importera

  1. Utwórz wystąpienie niestandardowe klasy ContentImporter.

  2. Przesłoń właściwość FileTypes i zwróć typy plików/rozszerzenia specyficzne dla technologii.

  3. Zastąp metodę CreateDeploymentType, aby utworzyć niestandardowy typ wdrożenia, importując wymagane właściwości z pliku zawartości.

  4. Zastąp metodę UpdateDeploymentType, aby zaktualizować typ instalatora niestandardowego przy użyciu ustawień z pliku zawartości.

  5. Funkcje pomocnika: Uwzględnij w celu zapewnienia kompletności i czytelności kodu to dwie funkcje pomocnicze. ProcessFileLine to funkcja pomocnicza o nazwie z metody UpdateDeploymentType do przetwarzania każdego wiersza pliku zawartości i ładowania wartości do niestandardowego typu instalatora. ParseType to funkcja pomocnicza o nazwie z funkcji pomocnika ProcessFileLine. Funkcja pomocnika ParseType analizuje oznaczenie typu pliku rdp i konwertuje je na typ platformy .NET.

Aby zdefiniować niestandardowego importera zawartości

  1. Utwórz wystąpienie Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter klasy przy użyciu konstruktora Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter .

    W poniższym przykładzie z przykładowego projektu RDP pokazano, jak zdefiniować importera zawartości.

    //   Content importer for .rdp files used by the RDP installer.   
    public class RdpContentImporter : ContentImporter  
    
  2. Zastąp właściwość Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter.FileTypes i zdefiniuj typy plików obsługiwane przez importera zawartości.

    W poniższym przykładzie z przykładowego projektu RDP pokazano, jak zastąpić właściwość FileTypes.

    // File types supported by the content importer.   
    public override IList<FileType> FileTypes  
    {  
        get  
        {  
             return Common.RdpFileTypes;   
        }  
    }  
    

    W przykładowym projekcie RDP wartość FileTypes jest definiowana w klasie Common projektu.

    //   This defines the file extensions supported by this content importer.   
    internal static readonly FileType[] RdpFileTypes = new[] { new FileType { Name = "RDP", Description = "Remote Desktop Configuration profile", Extensions = new[] { "rdp" } } };  
    
  3. Zastąp Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter.CreateDeploymentType metodę zarządzania tworzeniem niestandardowego typu wdrożenia przez zaimportowanie wymaganych właściwości z pliku zawartości.

    W poniższym przykładzie z przykładowego projektu RDP pokazano, jak zastąpić metodę CreateDeploymentType.

    // Creates a new deployment type from a content file.   
    public override DeploymentType CreateDeploymentType(string contentFile, object context)   
    {  
        Validator.CheckForNull(contentFile, "contentFile");  
        DeploymentType deploymentType = new DeploymentType(Common.TechnologyId) { Title = Path.GetFileNameWithoutExtension(contentFile) };  
        UpdateDeploymentType(deploymentType, contentFile);   
        return deploymentType;   
    }  
    

    W przykładowym projekcie RDP parametr ciągu DeploymentType jest zdefiniowany w klasie Common projektu lokalnego.

    // Internal ID of the technology.   
    public const string TechnologyId = "Rdp";  
    
  4. Zastąp Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter.UpdateDeploymentType metodę, aby zaktualizować typ wdrożenia przy użyciu ustawień z pliku zawartości.

    W poniższym przykładzie z przykładowego projektu RDP pokazano, jak zastąpić metodę UpdateDeploymentType.

    // Updates an existing deployment type installer with settings from content file.   
    public override void UpdateDeploymentType(DeploymentType dt, string contentFile, object context)   
    {  
        Validator.CheckForNull(dt, "dt");  
        Validator.CheckForNull(contentFile, "contentFile");  
                    RdpInstaller installer = dt.Installer as RdpInstaller;   
        if (null == installer)   
        {  
            throw new ArgumentOutOfRangeException("dt", dt.Installer, @"Installer type is not supported by this content importer.");   
        }  
        if (false == File.Exists(contentFile))   
        {  
            throw new FileNotFoundException("Content file was not found: " + contentFile, contentFile);   
        }  
        string[] fileText = File.ReadAllLines(contentFile);   
        fileText.ForEach(l => ProcessFileLine(l, installer));   
        installer.Filename = Path.GetFileName(contentFile);   
        installer.Contents.Clear();  
        installer.Contents.Add(new Content());  
        installer.Contents[0].Files.Add(new ContentFile());  
        installer.Contents[0].Location = Path.GetDirectoryName(contentFile);   
        installer.Contents[0].Files[0].Name = Path.GetFileName(contentFile);   
        return;   
    }  
    
  5. ProcessFileLine to funkcja pomocnicza o nazwie z metody UpdateDeploymentType do przetwarzania każdego wiersza pliku zawartości i ładowania wartości do niestandardowego typu instalatora.

    //   Processes a line in an .rdp file to set corresponding RdpInstaller properties.  
    private static void ProcessFileLine(string line, RdpInstaller installer)   
    {      
    if (string.IsNullOrEmpty(line))   
        {  
            Trace.TraceInformation("Skipping line, it is empty.");   
            return;   
        }  
        string[] elements = line.Split(':');   
        if (elements.Length < 2)   
        {  
            Trace.TraceError("Unexpected elements length: {0}. Skipping.", elements.Length);   
            return;   
        }  
        string elementName = elements[0];   
        Type elementType = ParseType(elements[1]);   
        string elementValueAsString = (elements.Length == 3) ? elements[2] : string.Empty;   
        object elementValue = (false == string.IsNullOrEmpty(elementValueAsString)) ? Convert.ChangeType(elementValueAsString, elementType) : null;   
        Trace.TraceInformation("Name: {0} Type: {1} Value: {2} ({3})", elementName, elementType, elementValue, elementValueAsString);   
        if (string.IsNullOrEmpty(elementValueAsString) || null == elementValue)   
        {  
            Trace.TraceWarning("Could not parse element value. Skipping.");   
            return;   
        }  
        switch (elementName.ToLowerInvariant())  
        {  
            case "username":  
                installer.Username = elementValueAsString;   
                break;   
            case "redirectprinters":  
                installer.RedirectPrinters = (1 == (int)elementValue);   
                break;   
            case "redirectsmartcards":  
                installer.RedirectSmartCards = (1 == (int)elementValue);   
                break;   
            case "alternate shell":  
                installer.RemoteApplication = elementValueAsString;   
                break;   
            case "keyboardhook":  
                installer.KeyboardMode = (RdpKeyboardMode)(int)elementValue;   
                break;   
            case "audiomode":  
                installer.AudioMode = (RdpAudioMode)(int)elementValue;   
                break;   
            case "desktopheight":  
                installer.DesktopHeight = (int)elementValue;   
                break;   
            case "desktopwidth":  
                installer.DesktopWidth = (int)elementValue;   
                break;   
            case "full address":  
                installer.FullAddress = elementValueAsString;   
                string[] tokens = elementValueAsString.Split(':');   
                ushort port = 0;   
                if (tokens.Length > 1)   
                {  
                    bool canParse = ushort.TryParse(tokens[1], out port);   
                    if (false == canParse)   
                    {  
                        Trace.TraceError("Improperly formatted port. Skipping.");   
                    }  
                    installer.RemoteServerPort = port;   
                }  
                installer.RemoteServerName = tokens[0];   
                break;   
            case "screen mode id":  
                int screenMode = (int)elementValue;   
                if (screenMode == 1 || screenMode == 2)   
                {  
                    installer.FullScreen = (1 == screenMode) ? false : true;   
                }  
                else  
                {  
                    Trace.TraceError("Invalid screen mode: {0}. Skipping.", screenMode);   
                }  
                break;   
            default:   
                Trace.TraceWarning("Unrecognized property: {0}. Skipping", elementName);   
                break;   
        }  
    }  
    

    ParseType to funkcja pomocnicza o nazwie z funkcji pomocnika ProcessFileLine. Funkcja pomocnika ParseType analizuje oznaczenie typu pliku rdp i konwertuje je na typ platformy .NET.

    //   Helper Function: Parses a .rdp file type designation and converts into a .NET Type.   
    private static Type ParseType(string type)   
    {      
    switch (type.ToLowerInvariant())  
        {  
            case "s":  
                return typeof(string);   
            case "i":  
                return typeof(int);   
            case "b":  
                return typeof(string);   
            default:   
                Trace.TraceInformation("Unrecognized type: {0}.", type);   
                return typeof(string);   
        }  
    }  
    

Obszary nazw

Microsoft. ConfigurationManagement.ApplicationManagement

Microsoft. ConfigurationManagement.ApplicationManagement.Serialization

Zestawy

Microsoft.ConfigurationManagement.ApplicationManagement.dll

Zobacz też

dokumentacja Configuration Manager