-2

here is a code to list the files I've in a directory, then the user can type the file name to open the file.

public static void openFile()
        {
            // List files in FormatedDocuments directory
            String[] showFiles = Directory.GetFiles("FormatedDocuments");

            int filesList = showFiles.GetUpperBound (0) + 1;
            const String folderToOpen = @"FormatedDocuments/";

            Console.WriteLine ("Here is the list of files:");
            for (int i = 0; i < filesList; i++) {
                Console.WriteLine ("\tFile : " + Path.GetFileName (showFiles [i]));
            }

            // When listing is finished, ask the user to select the file he want to open
            Console.WriteLine (@"Type the filename (With extension) you want to open:");
            String userChoice = folderToOpen + Console.ReadLine ();
            Process.Start (userChoice); // Loading with default application regarding the file extension

        }

My questions are:

  1. How to list only visible files in the selected directory? [DONE]

  2. How to return in the console a number before each file, and ask the user to type this number instead of the full file name? [Waiting proposition]

I'm a beginner and try to learn by myself, don't be too "expert" in your solution please, I know my current code is not optimized, I try to do it step by step, but I accept your help about this code :)

Thank you for your answers.

1 Answers1

2

I have found this:

This should work for you:

 DirectoryInfo directory = new DirectoryInfo(@"C:\temp"); 
 FileInfo[] files = directory.GetFiles();

 var filtered = files.Select(f => f)
                     .Where(f => (f.Attributes & FileAttributes.Hidden) == 0);

 foreach (var f in filtered) {
     Debug.WriteLine(f); 
 }
Community
  • 1
  • 1
Thealon
  • 1,935
  • 1
  • 13
  • 22
  • Hi, I've adapted this code to my code but is it possible to add a number in front of each line, and type this number to open the specified file instead of typing the full filename? Thank you. – Tarax Kawai Apr 23 '15 at 16:22
  • Sure you can simply make an int above the foreach loop and after debug.writeline (yourvariablename++) then change the debug.writeline in debug.writeline(yourvariablename + f) – Thealon Apr 24 '15 at 07:51
  • I've try multiple code but I'm unable to use the listing code and return a number in front of each file listed, plus use this number to call the process.start associated to the file. – Tarax Kawai Apr 24 '15 at 17:17