C#列出没有隐藏文件的文件(加上列出选项)

C#列出没有隐藏文件的文件(加上列出选项),c#,C#,下面是一个代码,列出我在目录中的文件,然后用户可以键入文件名来打开该文件 public static void openFile() { // List files in FormatedDocuments directory String[] showFiles = Directory.GetFiles("FormatedDocuments"); int filesList = showFiles.Get

下面是一个代码,列出我在目录中的文件,然后用户可以键入文件名来打开该文件

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

        }
publicstaticvoidopenfile()
{
//列出FormattedDocuments目录中的文件
字符串[]showFiles=Directory.GetFiles(“格式化文档”);
int filesList=showFiles.GetUpperBound(0)+1;
常量字符串folderToOpen=@“FormattedDocuments/”;
Console.WriteLine(“这是文件列表:”);
对于(int i=0;i
我的问题是:

  • 如何仅列出选定目录中的可见文件?[完成]

  • 如何在控制台中在每个文件前返回一个数字,并要求用户键入该数字而不是完整的文件名?[等待提议]

  • 我是一个初学者,尝试自己学习,请不要对您的解决方案太“专家”,我知道我当前的代码没有优化,我尝试一步一步地做,但我接受您关于此代码的帮助:)

    谢谢您的回答。

    我发现:

    这应该适合您:


    您好,我已将此代码改编为我的代码,但是否可以在每行前面添加一个数字,然后键入此数字以打开指定的文件,而不是键入完整的文件名?谢谢。当然你可以在foreach循环上面加一个int,然后在debug.writeline(yourvariablename++)之后,在debug.writeline(yourvariablename+f)中更改debug.writeline。我尝试了多个代码,但我无法使用列表代码并在列出的每个文件前面返回一个数字,另外,使用此号码调用与文件关联的process.start。
     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); 
     }