Any code samples to list arcgis layer files from a directory so they can be looped over?

3 Answers 3


Using a windows form, button, and listbox you could use this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' make a reference to a directory
            Dim di As New IO.DirectoryInfo("c:\temp")
            Dim diar1 As IO.FileInfo() = di.GetFiles("*.lyr*")
            Dim dra As IO.FileInfo

            'list the names of all files in the specified directory
            For Each dra In diar1

        Catch ex As Exception
            MessageBox.Show("Error " & ex.ToString)
        End Try

    End Sub

This should be easy to convert to C#, it is still .NET. Make sure to mark what artwork posted as the answer if it gives you the expected results.

  DirectoryInfo di = new DirectoryInfo(@"c:\temp");
  FileInfo diar1 = new di.GetFiles("*.lyr*");

  foreach(FileInfo dra in diar1)
catch(Exception ex)
   //do something with exception

The following function goes through a directory, gets a list of all the XML files, sorts the list by name, then writes the filenames of the XML files out to a text file. It does a little more than you need, but the principal is there of looping through the directory and doing something with the files - just another way to go about it.

public static void make_enerdeq_filelist(Constants c)
// Create Enerdeq_Filelist.txt to feed our ETL job
    // Kill Enerdeq_Filelist.txt if it exists
    if (File.Exists(c.infaServer + c.fileList))
        File.Delete(c.infaServer + c.fileList);
    // Sort files by name-alpha, INFA wants sorted input
    DirectoryInfo dir = new DirectoryInfo(c.infaServer);
    StreamWriter sw = File.CreateText(c.infaServer + c.fileList);
    string[] xml_files = Directory.GetFiles(c.infaServer);
    IComparer comp = new FileComparer(FileComparer.CompareBy.Name);
    Array.Sort(xml_files, comp);
    foreach (string xml_file in xml_files)
        FileInfo fi = new FileInfo(xml_file);
        string file_name = fi.Name;
        if (file_name.EndsWith(".xml"))  // XML files only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.