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.
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 } } } }
Comment