Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

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

        Try
            ' 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
                ListBox1.Items.Add(dra.Name)
            Next

        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#, developerfusion.com/tools/convert/vb-to-csharp –  artwork21 Oct 10 '11 at 12:45
add comment

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.

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

  foreach(FileInfo dra in diar1)
  {
    ListBox1.Items.Add(dra.Name);
  }
}
catch(Exception ex)
{
   //do something with exception
}
share|improve this answer
add comment

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
        {
            sw.WriteLine(file_name);
            sw.Flush();
        }
    }
    sw.Close();
}
share|improve this answer
add comment

Your Answer

 
discard

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.