自定义 XML 示例

note备注
因为本主题中的表格很宽,你可能需要调整其窗口宽度。

本主题内容:

  • 示例 1:迁移不受支持的应用程序

  • 示例 2:迁移“My Videos”文件夹

  • 示例 3:迁移文件和注册表项

  • 示例 4:从各个位置迁移特定文件夹

示例 1:迁移不受支持的应用程序

下面是一个可在需要迁移自己的应用程序的部分中使用的模板。它本身不能运行,但是你可以使用它编写自己的 .xml 文件。

<migration urlid="https://www.microsoft.com/migration/1.0/migxmlext/migtestapp">
  <component type="Application">
    <!-- Name of the application -->
    <displayName>Some Application</displayName>
    <!-- Specify whether the environment variables exist in the context of user or system or both -->
    <environment context="System">
      <!-- Create the environment variables -->
      <variable name="myVar1">
        <!-- Simple text value assignment to a variable -->
        <text>value</text>
      </variable>
      <variable name="myAppExePath">
        <!-- Make a call to in-built helper function to get a value from a reg key and assign that value to the variable -->
        <script>MigXMLHelper.GetStringContent("Registry","HKLM\Software\MyApp\Installer [EXEPATH]")</script>
      </variable>
    </environment>
    <role role="Settings">
      <detects>
        <!-- All of these checks must be true for the component to be detected -->
        <detect>
          <!-- Make a call to in-built helper function to check if an object exists or not -->
          <condition>MigXMLHelper.DoesObjectExist("Registry","HKLM\Software\MyApp [win32_version]")</condition>
        </detect>
        <detect>
          <!-- Either of these checks must be true for the component to be detected -->
          <!-- Make a call to in-built helper function to check if a file version matches or not -->
          <condition>MigXMLHelper.DoesFileVersionMatch("%MyAppExePath%","ProductVersion","8.*")</condition>
          <condition>MigXMLHelper.DoesFileVersionMatch("%MyAppExePath%","ProductVersion","9.*")</condition>
        </detect>
      </detects>
      <!-- Describe the rules that will be executed during migration of this component and the context, whether user, system or both -->
      <rules context="User">
        <!-- Delete objects specified in the object set on the destination computer before applying source objects -->
        <destinationCleanup>
          <!-- Describe the pattern for the list of objects to be deleted -->
          <objectSet>
            <pattern type="Registry">HKCU\Software\MyApp\Toolbar\* [*]</pattern>
            <pattern type="Registry">HKCU\Software\MyApp\ListView\* [*]</pattern>
            <pattern type="Registry">HKCU\Software\MyApp [ShowTips]</pattern>
          </objectSet>
        </destinationCleanup>
        <!-- Specify which set of objects should be migrated -->
        <include>
          <!-- Describe the pattern for the list of objects to be included -->
          <objectSet>
            <pattern type="Registry">HKCU\Software\MyApp\Toolbar\* [*]</pattern>
            <pattern type="Registry">HKCU\Software\MyApp\ListView\* [*]</pattern>
            <pattern type="Registry">HKCU\Software\MyApp [ShowTips]</pattern>
          </objectSet>
        </include>
        <!-- Specify which set of objects should not be migrated -->
        <exclude>
          <!-- Describe the pattern for the list of objects to be excluded from migration -->
          <objectSet>
            <pattern type="Registry">HKCU\Software\MyApp [Display]</pattern>
          </objectSet>
        </exclude>
      </rules>
    </role>
  </component>
</migration>

示例 2:迁移“My Videos”文件夹

下面是一个名为 CustomFile.xml 的自定义 .xml 文件,该文件用于为所有用户迁移“My Videos”(如果源计算机上存在该文件夹)。

代码 行为
<condition>MigXmlHelper.DoesObjectExist("File","%CSIDL_MYVIDEO%")</condition>

验证源计算机上是否存在“My Videos”。

<include filter='MigXmlHelper.IgnoreIrrelevantLinks()'>

筛选出不在目标计算机上解析的“My Videos”中的快捷方式。此操作对不是快捷方式的文件没有影响。例如,如果源计算机上的“My Videos”中存在指向 C:\Folder1 的快捷方式,则仅当目标计算机上存在 C:\Folder1 时才迁移该快捷方式。但是,系统会不加筛选地迁移所有其他文件(例如 .mp3 文件)。

<pattern type="File">%CSIDL_MYVIDEO%\* [*]</pattern>

为所有用户迁移“My Videos”。

<?xml version="1.0" encoding="UTF-8"?>
<migration urlid="https://www.microsoft.com/migration/1.0/migxmlext/CustomFile">
<component type="Documents" context="User">
        <displayName>My Video</displayName>
        <role role="Data">
            <detects>           
                <detect>
                    <condition>MigXmlHelper.DoesObjectExist("File","%CSIDL_MYVIDEO%")</condition>
                </detect>
            </detects>
            <rules>
                <include filter='MigXmlHelper.IgnoreIrrelevantLinks()'>
                    <objectSet>
                        <pattern type="File">%CSIDL_MYVIDEO%\* [*]</pattern>
                    </objectSet>
                </include>
           </rules>
        </role>
    </component>
</migration>

示例 3:迁移文件和注册表项

本表描述了以下示例 .xml 文件中的行为。

代码 行为
<pattern type="File">%ProgramFiles%\USMTTestFolder\* [USMTTestFile.txt]</pattern>

从 %ProgramFiles%\USMTTestFolder 下的所有子目录迁移 Usmttestfile.txt 文件的所有实例。

<pattern type="File">%ProgramFiles%\USMTDIRTestFolder\* [*]</pattern>

迁移 %ProgramFiles%\USMTDIRTestFolder 下的整个目录。

<pattern type="Registry">HKCU\Software\USMTTESTKEY\* [MyKey]</pattern>

迁移 HKCU\Software\USMTTESTKEY 下的 MyKey 的所有实例。

<pattern type="Registry">HKLM\Software\USMTTESTKEY\* [*]</pattern>

迁移 HKLM\Software\USMTTESTKEY 下的整个注册表配置单元。

<migration urlid="https://www.microsoft.com/migration/1.0/migxmlext/testfilemig">
  <component type="Application" context="System">
   <displayName>File Migration Test</displayName>
   <role role="Data">
    <rules context="System">
     <include>
      <objectSet>
        <pattern type="File">%ProgramFiles%\USMTTestFolder\* [USMTTestFile.txt]</pattern><pattern type="File">%ProgramFiles%\USMTDIRTestFolder\* [*]</pattern>
      </objectSet>
    </include>
   </rules>
  </role>
</component>
<component type="System">
  <displayName>Registry Migration Test</displayName>
  <role role="Settings">
   <rules context="UserAndSystem">
     <include>
      <objectSet>
          <pattern type="Registry">HKCU\Software\USMTTESTKEY\* [MyKey]</pattern>
          <pattern type="Registry">HKLM\Software\USMTTESTKEY\* [*]</pattern>
      </objectSet>
     </include>
   </rules>
  </role>
 </component>
</migration>

示例 4:从各个位置迁移特定文件夹

在代码的 <displayName> 标记中介绍这种自定义 .xml 文件的行为。

<migration urlid="https://www.microsoft.com/migration/1.0/migxmlext/test">

<component type="Documents" context="System">
  <displayName>Component to migrate all Engineering Drafts subfolders without documents in this folder </displayName>
  <role role="Data">
    <rules>
         <include>
            <objectSet>
                 <pattern type="File"> C:\EngineeringDrafts\* [*]</pattern>
            </objectSet>
          </include>
         <exclude>
            <objectSet>
                 <pattern type="File"> C:\EngineeringDrafts\ [*]</pattern>
            </objectSet>
         </exclude>
    </rules>
  </role>
</component>

<component type="Documents" context="System">
  <displayName>Component to migrate all user documents except Sample.doc</displayName>
  <role role="Data">
    <rules>
         <include>
            <objectSet>
                 <pattern type="File"> C:\UserDocuments\* [*]</pattern>
            </objectSet>
          </include>
        <exclude>
             <objectSet>
                 <pattern type="File"> C:\UserDocuments\ [Sample.doc]</pattern>
             </objectSet>
          </exclude>
    </rules>
  </role>
</component>

<component type="Documents" context="System">
  <displayName>Component to migrate all Requests folders on any drive on the computer </displayName>
  <role role="Data">
    <rules>
         <include>
            <objectSet>
         <script>MigXmlHelper.GenerateDrivePatterns ("\Requests\* [*] ", "Fixed")</script>            
         <script>MigXmlHelper.GenerateDrivePatterns ("*\Requests\* [*] ", "Fixed")</script>            
     </objectSet>
          </include>
    </rules>
  </role>
</component>

<component type="Documents" context="System">
  <displayName>Component to migrate all Presentations folder from any location on the C: drive </displayName>
  <role role="Data">
    <rules>
         <include>
            <objectSet>                 
<pattern type="File"> C:\*\Presentations\* [*]</pattern>
<pattern type="File"> C:\Presentations\* [*]</pattern>
           </objectSet>
          </include>
    </rules>
  </role>
</component>
</migration>

另请参阅

其他资源

USMT XML 参考
自定义 USMT XML 文件