0

I am using C# and was wondering if there is a way to ask the user for a file name (in the console application) within the local documents. I don't really know how to structure this question when looking online but the code I have :

   static void Main(string[] args)
    {

        string[] random1 = System.IO.File.ReadAllLines(@"//..Random1.txt");

        foreach (string r1 in random1)
        {
            Console.WriteLine(r1);
        }

But ideally I would want something like:

static void Main(string[] args)
    {
        Console.WriteLine("Enter the name of the file you would like to see")
        // Following the input let's say " Potato.txt " is entered

        string[] chosenFile1 = System.IO.File.ReadAllLines(@"//..Potato.txt");

        foreach (string file in chosenFile1)
        {
            Console.WriteLine(file);
        }

I am not entirely sure how to go about it as usually its dependant on the path of the files , but I thought this way would be more appropriate as different users from different devices can try this out. Hope this makes sense , all help appreciated.

GHOSTROBOT
  • 11
  • 2
  • You can pass file name as argument to your executable. > ./yourexe 'c:\pathtofile' and in your code you can access it https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments – adt Mar 03 '20 at 11:33

2 Answers2

0

I would recommend you ask user not for a filename but for a file number. Filename can be very long and, commonly, user doesn't have a necessity to input it if you as a programmer can provide some kind of the menu.

using System.IO;


    public static void Main(string[] args)
    {
        const string dir = @"e:\2";  // your folder here
        var files = Directory.GetFiles(dir);
        for (var i = 0; i < files.Length; i++)
        {
            Console.WriteLine($"{i}) {Path.GetFileName(files[i])}");
        }

        while (true)
        {
            Console.WriteLine("Enter the number of the file you would like to see or -1 to exit");
            var choice = Console.ReadLine();

            if (int.TryParse(choice, out var index) && index > -2 && index < files.Length)
            {
                if (index == -1)
                    return;

                string[] chosenFile1 = System.IO.File.ReadAllLines(files[index]);
                foreach (string file in chosenFile1)
                {
                    Console.WriteLine(file);
                }
            }
            else
                Console.WriteLine("Bad input. Repeat please.");
        }
    } 
Miamy
  • 2,162
  • 3
  • 15
  • 32
  • Thanks I will try this. Really quickly too , having set all that , if the files chosen by the user contain the exact same information , why won't ' if (random1 == random2) ' work? – GHOSTROBOT Mar 03 '20 at 12:50
  • Because `random1` and `random2` are different *objects*, though they have the same values. – Miamy Mar 03 '20 at 12:55
  • If you need to compare two arrays, please refer to https://stackoverflow.com/questions/3232744/easiest-way-to-compare-arrays-in-c-sharp, for example. – Miamy Mar 03 '20 at 13:07
-1

You can pass file name to your .exe when you execute this from terminal (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) or use Console.ReadLine to read the input in your program (https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netframework-4.8)

gamusino
  • 1
  • 2