Displaying files in a Folder, then choosing one...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lovro Mirnik
    New Member
    • Feb 2011
    • 8

    Displaying files in a Folder, then choosing one...

    Greetings,

    I need some assistance.
    My goal is to create a program which does the following.
    Checks for a Folder ( C:\ExampleFolde r\ )
    If it exists, it checks for any files in the Folder ( Baby.txt )
    If it does not, creates it.

    If there are any files in the folder, it displays all of them in a list, with an added number. ( 1,2,3,4... )
    Then asks me, to which one I'd like to write to.
    I'll enter "Baby" and StreamWriter will do the rest.

    I am using foreach for the listing operation, but that's not helping me. I can only choose a file to write to when it's displayed, and not after that. I would like to see all the files being displayed in a List. Then I would choose! I need a good solution for this problem, or merely an advice I can follow. Any help will be greatly appreciated.
    My problem is also explained in the code, so please take a look.
    Code:
    /* Read through the script before you run it!
     * That way will save you from losing any data.
     */
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ExampleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                int ExampleNumber; // For numbering files
                string ExamplePath = @"C:\ExampleFolder\"; // The folder path
                string ExampleExtension = ".txt"; // Self-explanatory
                Directory.CreateDirectory(ExamplePath); // Creates the folder on your C:\ disk. WARNING! Make sure there is no such folder already existent on your drive! This command overwrites it!!
                StreamWriter ExampleFileBaby = new StreamWriter(@"C:\ExampleFolder\Baby.txt"); // Creates the three files
                StreamWriter ExampleFileSeven = new StreamWriter(@"C:\ExampleFolder\Seven.txt");
                StreamWriter ExampleFileTest_1 = new StreamWriter(@"C:\ExampleFolder\Test_1.txt");
                if (Directory.Exists(ExamplePath) == true) // If the Folder exists
                {
                    ExampleNumber = 0; // I define this variable's value here. I could define it right after I've declared it!
                    string[] ExampleFiles = Directory.GetFiles(ExamplePath, "*" + ExampleExtension);
                    // Returns every .txt file in the Folder
                    // In here I should also add a check, wheter there are any files in the folder...
                    // But I wanted to keep it simple. So since we know they are, we continue!
                    foreach (string ExampleFileNames in ExampleFiles)
                    {
                        ExampleNumber = ExampleNumber + 1; // For each .txt file in the Folder increase the value by 1
                        Console.WriteLine(ExampleNumber + " " + ExampleFileNames);
                        // An example output would be:
                        // 1 Baby.txt
                        // 2 Seven.txt
                        // 3 Test_1.txt
                        // 
                        // !! How do it then call my StreamWriter to write in a specific file? !!
                        // ----------------------------------------------------------------------
                        // I try like this...
                        Console.Write(" Which file would you like to write to? : ");
                        string ExampleFileSelect = Console.ReadLine(); // Reads my input
                        if (ExamplePath + ExampleFileSelect + ExampleExtension == ExampleFileNames) // If my input equals any of these (Baby, Seven, Test_1) files ???????
                        {
                            Console.WriteLine("You've chosen : " + ExampleFileSelect + "."); // Tell me again which one I've chosen!
                            Console.WriteLine("You've chosen : " + ExampleFileNames + ".");  // This one should do the same...
                        }
                        else
                        {
                            Console.WriteLine("Does not work!"); // No good...
                        }
                        // The output now is:
                        // -----------------------------------------------
                        // 1 C:\ExampleFolder\Baby.txt
                        // Which file would you like to write to? : "Baby"
                        // You've chosen : Baby.
                        // You've chosen : C:\ExampleFolder\Baby.txt
                        // 2 C:\ExampleFolder\Seven.txt
                        // Which file would you like to write to? : "Baby"
                        // Does not work!
                        // 2 C:\ExampleFolder\Test_1.txt
                        // Which file would you like to write to? : "Baby"
                        // Does not work!
                        // ------------------------------------------------
                        //
                        // As you can see, I can only choose one, after it's been displayed.
                        // How then, can I make that the list first displays every possible folder
                        // Then lets me choose which one?
                        // Every suggestion will be helpful!
                    }
                    Console.ReadKey(); // Of course, waits for Input.
                }
                if (Directory.Exists(ExamplePath) == false) // If the Folder doesn't exist (In this case it does!)
                {
                    // Create the folder
                }
            }
        }
    }
    Attached Files
  • Leito
    New Member
    • Apr 2010
    • 58

    #2
    Hey,

    Here's what I would do:

    - Save the files list in an ArrayList
    - Display the files (using foreach as you have done)
    - Ask for the file to write in (as you have done)
    - Check for the file (use ArrayList.Conta ins() method)

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Here's a big debugging tip. You can set a breakpoint on the line 52 (above). When that breakpoint is hit, you can see what the data is and find out exactly why it's failing.

      That said, I'm willing to bet it's 'cause you're doing the input part inside the loop that displays the output. Not only are you interrupting the listing of the files with your input, but you're only comparing the input to the current item in the list. Let the listing loop finish first, then enter into a new loop to handle the input.

      Leito suggests using an ArrayList and I think this is appropriate, mostly. I'd suggest using a generic List<T> class instead, that way it's typed to all the same type, which is appropriate for your case. This object also has a Contains method. You can also add your array of strings to it quite easly... example:

      Code:
      string[] files = Directory.GetFiles(...);
      List<string> fileList = new List<string>();
      fileList.AddRange(files);
      Finally,

      // In here I should also add a check, wheter there are any files in the folder...
      // But I wanted to keep it simple. So since we know they are, we continue!
      You should do this check... I think GetFiles might return null (instead of an empty array) when there are no files. Foreach will crash when it tries to process the list (since it can't access the GetEnumerator method on a null object). At a minimum, you should put a null check after GetFiles. After that, you can include an additional check for an empty list if you wish.

      Comment

      • Lovro Mirnik
        New Member
        • Feb 2011
        • 8

        #4
        Thank you both for answering. I'm sure I'll work something out now...
        Since Gary's answer was more informative, it should by all right be chosen as the best answer.

        But, I'll chose Leito's. It's simpler, and more beginners can relate to it.

        Lovro Mirnik

        Comment

        Working...