2

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

artwork21
35.2k8 gold badges69 silver badges135 bronze badges
asked Oct 10, 2011 at 11:45

3 Answers 3

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
 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
answered Oct 10, 2011 at 12:12
1
3

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
}
answered Oct 10, 2011 at 12:39
1

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();
}
answered Oct 10, 2011 at 14:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.