【问题标题】:C# Listing files without hidden files (Plus listing option) [duplicate]C# 列出没有隐藏文件的文件(加上列表选项)[重复]
【发布时间】:2015-04-23 11:50:03
【问题描述】:

这是列出我在目录中的文件的代码,然后用户可以键入文件名来打开文件。

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

        }

我的问题是:

  1. 如何仅列出所选目录中的可见文件? [完成]

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

我是初学者并尝试自学,请不要对您的解决方案过于“专家”,我知道我当前的代码没有优化,我尝试逐步进行,但我接受您的关于这段代码的帮助:)

感谢您的回答。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我找到了this:

    这应该适合你:

     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); 
     }
    

    【讨论】:

    • 您好,我已将此代码改编为我的代码,但是否可以在每行前面添加一个数字,然后输入此数字来打开指定的文件,而不是输入完整的文件名?谢谢。
    • 当然你可以简单地在 foreach 循环上方和 debug.writeline (yourvariablename++) 之后创建一个 int 然后在 debug.writeline(yourvariablename + f) 中更改 debug.writeline
    • 我尝试了多个代码,但我无法使用列表代码并在列出的每个文件前面返回一个数字,并使用此数字调用与文件关联的 process.start。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2017-04-03
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多