Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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
share|improve this answer
Thanks. I am needing c#..forgot to mention that. – Justin Oct 10 '11 at 12:17
@Justin, You can use this website to convert between vb and c#, – artwork21 Oct 10 '11 at 12:45

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
share|improve this answer

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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