locked
Helping creating an array of files RRS feed

  • Question

  • I am making an application that puts a list of .txt files into a string array and then puts the string array list of .txt files into a list box. I am having problems assigning them to a string array however. Here is my code..

     

    Code Snippet

    public void FindReadFiles()

    {

    DirectoryInfo dir = new DirectoryInfo("SavedWebsiteInformation");

    FileInfo[] txtFiles = dir.GetFiles("*.txt");

    string[] FileArray;

     

    foreach (FileInfo file in txtFiles)

    {

    for (int i = 0; i < txtFiles.Length; i++)

    {

    FileArray[i] = file.Name.ToString();

    }

    }

    }

     

     

    When I execute this code it compiles but I get a NullRefrenceException pointing at the line saying "FileArrayIdea = file.Name.ToString().

    However..if I do the following code..

     

    Code Snippet

    public void FindReadFiles()

    {

    DirectoryInfo dir = new DirectoryInfo("SavedWebsiteInformation");

    FileInfo[] txtFiles = dir.GetFiles("*.txt");

    string HoldFile = "No Files Found";

    foreach (FileInfo file in txtFiles)

    {

    HoldFile = file.Name.ToString();

    }

    }

     

     

    It works fine but obviously the variable Holdfine keeps getting reassinged a file name and will only be the last file in the directory. Could someone please tell me how to assign these to a string array??
    Saturday, April 19, 2008 3:09 PM

Answers

  • It's because you have not initialised your string[] array - therefore you will get a nullreferenceexception because you are performing some operation to an object which has not been initalised.

     

    is there any reason why you are using the string[] array and not just using the fileinfo array?

     

    your solution right now is to do this:

     

    ...

    string[] fileArray = new string[txtFiles.Length];

     

     

    public void FindReadFiles()

    {

    DirectoryInfo dir = new DirectoryInfo("SavedWebsiteInformation");

    FileInfo[] txtFiles = dir.GetFiles("*.txt");

    string[] FileArray = new string[txtFiles.Length];

    int counter = 0;

     

    foreach (FileInfo file in txtFiles)

    {

          FileArray[counter] = file.Name;

          counter++;

    }

    }

     

     

    Hope this helps

    Saturday, April 19, 2008 3:47 PM

All replies

  • It's because you have not initialised your string[] array - therefore you will get a nullreferenceexception because you are performing some operation to an object which has not been initalised.

     

    is there any reason why you are using the string[] array and not just using the fileinfo array?

     

    your solution right now is to do this:

     

    ...

    string[] fileArray = new string[txtFiles.Length];

     

     

    public void FindReadFiles()

    {

    DirectoryInfo dir = new DirectoryInfo("SavedWebsiteInformation");

    FileInfo[] txtFiles = dir.GetFiles("*.txt");

    string[] FileArray = new string[txtFiles.Length];

    int counter = 0;

     

    foreach (FileInfo file in txtFiles)

    {

          FileArray[counter] = file.Name;

          counter++;

    }

    }

     

     

    Hope this helps

    Saturday, April 19, 2008 3:47 PM
  • Cheers mate. That fixed it perfectly. I am making it a string array becuase it's going straight into a listbox and I need the .txt file names to be strings when I read them.

    Saturday, April 19, 2008 3:57 PM
  • no worries.

     

    Well, if they are going directly into a listbox, do this:

     

     

    public void FindReadFiles()

    {

    DirectoryInfo dir = new DirectoryInfo("SavedWebsiteInformation");

    FileInfo[] txtFiles = dir.GetFiles("*.txt");

     

    foreach (FileInfo file in txtFiles)

    {

          this.listBox1.Items.Add(file.Name);

    }

    }

     

     

    And thats it.

    • Proposed as answer by Isaac Morris Thursday, June 23, 2011 2:59 AM
    Saturday, April 19, 2008 3:58 PM