This documentation is archived and is not being maintained.

TreeNodeCollection.CopyTo Method

Copies all the items from the TreeNodeCollection object to a compatible one-dimensional array of TreeNode objects, starting at the specified index in the target array.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public void CopyTo(
	TreeNode[] nodeArray,
	int index


Type: System.Web.UI.WebControls.TreeNode[]

A zero-based array of TreeNode objects that receives the copied items from the TreeNodeCollection.

Type: System.Int32

The position in the target array at which to start receiving the copied content.

Use the CopyTo method to copy the contents of the TreeNodeCollection into the specified zero-based System.Array object. Items are copied starting at the specified index of the target array. With the System.Array object, you can then use array syntax to access the items in the TreeNodeCollection.

As an alternative, you can also use the GetEnumerator method to create an enumerator that can be used to access the items in the collection.

The following example demonstrates how to use the CopyTo method to copy the values of a TreeNodeCollection into an array. Notice that the Nodes property returns a TreeNodeCollection object.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

  void Page_Load(Object sender, EventArgs e)

    // If the TreeView control contains any root nodes, display the  
    // text value of each node.  
    if (LinksTreeView.Nodes.Count > 0)

      // Declare an array of TreeNode objects.
      TreeNode[] RootNodeArray = new TreeNode[LinksTreeView.Nodes.Count];

      // Use the CopyTo method to copy the root nodes into the array.
      LinksTreeView.Nodes.CopyTo(RootNodeArray, 0);

      // Display the root nodes. 
      foreach (TreeNode node in RootNodeArray)

        Message.Text += node.Text + "<br />";



      Message.Text = "The TreeView control does not have any nodes.";




<html xmlns="" >
  <head runat="server">
    <title>TreeNodeCollection CopyTo Example</title>
    <form id="form1" runat="server">

      <h3>TreeNodeCollection CopyTo Example</h3>

      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"


          <asp:TreeNodeStyle ChildNodesPadding="10" 
          <asp:TreeNodeStyle ChildNodesPadding="5" 
          <asp:TreeNodeStyle ChildNodesPadding="5" 
          <asp:TreeNodeStyle ChildNodesPadding="10" 



          <asp:TreeNode Text="Table of Contents"

            <asp:TreeNode Text="Chapter One">

              <asp:TreeNode Text="Section 1.0">

                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>


              <asp:TreeNode Text="Section 1.1">

                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>



            <asp:TreeNode Text="Chapter Two">

              <asp:TreeNode Text="Section 2.0">

                <asp:TreeNode Text="Topic 2.0.1">

                  <asp:TreeNode Text="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>

                <asp:TreeNode Text="Topic 2.0.2"/>




          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />



      <br /><br />

      <asp:Label id="Message"


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0