This question already has answers here:
How to recursively list all the files in a directory in C#?
                                
                                    (19个回答)
                                
                        
                                6年前关闭。
            
                    
这是代码:

private void SearchForDoc()
        {
            t = Environment.GetEnvironmentVariable("UserProfile")+"\\documents";
            string[] s = Directory.GetFiles(t, "*.txt",);


这将在目录中搜索txt文件,但我也想在其子目录中找到所有文本文件。
“ * .txt”后面应有SearchOption searchOption,

最佳答案

使用以下重载:Directory.GetFiles Method (String, String, SearchOption),其中SearchOption是SearchOption.AllDirectories

string[] s = Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);

10-07 23:40