我想把指定文件下的文件名自动显示在listview上,以下可以显示前三个文件,并附在imagelist里面的图片下,我想动态的读取所有,不知道怎么写这个循环,恳请大家帮忙,我的代码如下:
            listView1.Items[0].ImageIndex = 0;
            listView1.Items[1].ImageIndex = 1;
            listView1.Items[2].ImageIndex = 2;  
            DirectoryInfo dir = new DirectoryInfo(@"\Storage Card");
            FileInfo[] files = dir.GetFiles();
            listView1.Items[0].Text = files[0].Name;
            listView1.Items[1].Text = files[1].Name;
            listView1.Items[2].Text = files[2].Name;

解决方案 »

  1.   

    //listView1.Items[0].ImageIndex = 0;
                //listView1.Items[1].ImageIndex = 1;
                //listView1.Items[2].ImageIndex = 2;  
                DirectoryInfo dir = new DirectoryInfo(@"d:\Debug");
                FileInfo[] files = dir.GetFiles();
                int i = 0;
                foreach (FileInfo file in files)
                {
                    System.Diagnostics.Debug.WriteLine(files[i].Name);
                    listView1.Items.Add(files[i].Name);
                  // listView1.Items[i].Text = files[i].Name;
                    i++;
                }            //listView1.Items[0].Text = files[0].Name;
                //listView1.Items[1].Text = files[1].Name;
                //listView1.Items[2].Text = files[2].Name;
      

  2.   

    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Collections;public class FindingExistingFilesAndDirectories{   // Retrieves an array of all directories in the store, and 
       // displays the results.   public static void Main(){      // This part of the code sets up a few directories and files in the
          // store.
          IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
          isoStore.CreateDirectory("TopLevelDirectory");
          isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
          isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
          new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
          new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
          // End of setup.      Console.WriteLine('\r');
          Console.WriteLine("Here is a list of all directories in this isolated store:");      foreach(string directory in GetAllDirectories("*", isoStore)){
             Console.WriteLine(directory);
          }
          Console.WriteLine('\r');      // Retrieve all the files in the directory by calling the GetFiles 
          // method.      Console.WriteLine("Here is a list of all the files in this isolated store:");
          foreach(string file in GetAllFiles("*", isoStore)){
             Console.WriteLine(file);
          }     }// End of Main.
      
       // Method to retrieve all directories, recursively, within a store.   public static string[] GetAllDirectories(string pattern, IsolatedStorageFile storeFile){      // Get the root of the search string.      string root = Path.GetDirectoryName(pattern);      if (root != "") root += "/";      // Retrieve directories.      string[] directories;      directories = storeFile.GetDirectoryNames(pattern);      ArrayList directoryList = new ArrayList(directories);      // Retrieve subdirectories of matches.      for (int i = 0, max = directories.Length; i < max; i++){
             string directory = directoryList[i] + "/";
             string[] more = GetAllDirectories (root + directory + "*", storeFile);         // For each subdirectory found, add in the base path.         for (int j = 0; j < more.Length; j++)
                more[j] = directory + more[j];         // Insert the subdirectories into the list and 
             // update the counter and upper bound.         directoryList.InsertRange(i+1, more);
             i += more.Length;
             max += more.Length;
          }      return (string[])directoryList.ToArray(Type.GetType("System.String"));
       }   public static string[] GetAllFiles(string pattern, IsolatedStorageFile storeFile){      // Get the root and file portions of the search string.      string fileString = Path.GetFileName(pattern);      string[] files;
          files = storeFile.GetFileNames(pattern);      ArrayList fileList = new ArrayList(files);      // Loop through the subdirectories, collect matches, 
          // and make separators consistent.      foreach(string directory in GetAllDirectories( "*", storeFile))
             foreach(string file in storeFile.GetFileNames(directory + "/" + fileString))
                fileList.Add((directory + "/" + file));      return (string[])fileList.ToArray(Type.GetType("System.String"));
             
       }// End of GetFiles.