TreeNodeCollection.System.Collections.ICollection.CopyTo Method

This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.

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

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

void ICollection.CopyTo (
	Array array,
	int index
Not applicable.



A zero-based System.Array object that receives the copied items from the TreeNodeCollection.


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

Exception typeCondition


array is not an array of TreeNode objects.

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 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions