ArcObjects Library Reference

List Files to StringBuilder Snippet

Obtain a listing of files for a specified directory and place them in a StringBuilder.

[C#]

///<summary>Obtain a listing of files for a specified directory and place them in a StringBuilder.</summary>
/// 
///<param name="directory">A System.String that is the directory for which you want to list files into the StringBuilder. Ex: "C:\temp"</param>
/// 
///<returns>A System.Text.StringBuilder</returns>
/// 
///<remarks></remarks>
public System.Text.StringBuilder ListFilesToStringBuilder(System.String directory)
{

  System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(directory);
  System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(2000);

  if (directoryInfo != null)
  {

	System.IO.FileInfo[] fileInfo = directoryInfo.GetFiles();
	System.Int32 i = 0;

	System.Int32 tempFor1 = fileInfo.GetUpperBound(0);
	for (i = 0; i <= tempFor1; i++)
	{

	  stringBuilder.Append(fileInfo[i].Name + System.Environment.NewLine);

	}

  }

  return stringBuilder;

}
[Visual Basic .NET]

'''<summary>Obtain a listing of files for a specified directory and place them in a StringBuilder.</summary>
''' 
'''<param name="directory">A System.String that is the directory for which you want to list files into the StringBuilder. Ex: "C:\temp"</param>
''' 
'''<returns>A System.Text.StringBuilder</returns>
''' 
'''<remarks></remarks>
Public Function ListFilesToStringBuilder(ByVal directory As System.String) As System.Text.StringBuilder

  Dim directoryInfo As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(directory)
  Dim stringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(2000)

  If Not (directoryInfo Is Nothing) Then

    Dim fileInfo As System.IO.FileInfo() = directoryInfo.GetFiles
    Dim i As System.Int32

    For i = 0 To fileInfo.GetUpperBound(0)

      stringBuilder.Append(fileInfo(i).Name + System.Environment.NewLine)

    Next i

  End If

  Return stringBuilder

End Function


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • System
  • System.Windows.Forms