TreeNodeCollection.CopyTo Method

Note: This method is new in the .NET Framework version 2.0.

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
public void CopyTo (
	TreeNode[] nodeArray, 
	int index
public function CopyTo (
	nodeArray : TreeNode[], 
	index : int



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


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#" %>

<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.";




    <form runat="server">
      <h3>TreeNodeCollection CopyTo Example</h3>
      <asp:TreeView id="LinksTreeView"
        Font-Name= "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" />

      <asp:Label id="Message"


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions