C# 使用LINQ访问文件夹

C# 使用LINQ访问文件夹,c#,.net,linq,C#,.net,Linq,我想使用LINQ访问特殊文件夹 var s1 = (from folder in new DirectoryInfo(@"C:\Users\" + Environment.UserName + Environment.SpecialFolder.Desktop).GetDirectories() select folder).ToList(); 我正在使用此代码,但无法检索数据您需要在环境.UserName和环境.SpecialFolde

我想使用LINQ访问特殊文件夹

var s1 = (from folder in new DirectoryInfo(@"C:\Users\" + Environment.UserName + Environment.SpecialFolder.Desktop).GetDirectories()  
                          select folder).ToList();

我正在使用此代码,但无法检索数据

您需要在
环境.UserName
环境.SpecialFolder.Desktop
之间添加反斜杠(
\

e、 g


您需要在
Environment.UserName
Environment.SpecialFolder.Desktop
之间添加反斜杠(
\

e、 g


要获取桌面文件夹中的所有文件,包括子文件夹中的所有文件, 我将使用以下代码:

 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  List<FileInfo> s1 = new DirectoryInfo(desktopPath).GetFiles("*.*", SearchOption.AllDirectories).ToList();
string desktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
List s1=new DirectoryInfo(desktopPath).GetFiles(“**”),SearchOption.AllDirectories.ToList();

这可能有助于u.

获取桌面文件夹中的所有文件,包括子文件夹中的任何文件, 我将使用以下代码:

 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  List<FileInfo> s1 = new DirectoryInfo(desktopPath).GetFiles("*.*", SearchOption.AllDirectories).ToList();
string desktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
List s1=new DirectoryInfo(desktopPath).GetFiles(“**”),SearchOption.AllDirectories.ToList();

这可能会对您有所帮助。

您的用户名和桌面之间缺少反斜杠:

@"C:\Users\" + Environment.UserName + Environment.SpecialFolder.Desktop
但它更容易使用:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

这将返回整个路径,无需硬编码
c:\users\

用户名和桌面之间缺少反斜杠:

@"C:\Users\" + Environment.UserName + Environment.SpecialFolder.Desktop
但它更容易使用:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
这将返回整个路径,无需硬编码
c:\users\

  • 似乎您应该尝试
    Path.Combine
  • Environment.SpecialFolder.Desktop
    不是字符串,因此必须将其转换为字符串
所以试试这个:

var desktop = System.IO.Path.Combine(@"C:\Users\", Environment.UserName, Environment.SpecialFolder.Desktop.ToString());
var result = (from folder in new DirectoryInfo(desktop).GetDirectories()
                  select folder).ToList();
  • 似乎您应该尝试
    Path.Combine
  • Environment.SpecialFolder.Desktop
    不是字符串,因此必须将其转换为字符串
所以试试这个:

var desktop = System.IO.Path.Combine(@"C:\Users\", Environment.UserName, Environment.SpecialFolder.Desktop.ToString());
var result = (from folder in new DirectoryInfo(desktop).GetDirectories()
                  select folder).ToList();
  • System.IO和Linq不是最好的朋友,因为您希望在文件级别处理异常,延迟执行和磁盘访问很困难
  • 使用
    Path
    类创建路径,例如
    Path.Combine(part1,part2,…)
  • 如果只需要路径,则不需要使用
    DirectoryInfo
    类,可以使用静态
    Directory
    File
    方法,如
    Directory.GetDirectories
  • 您可以通过
    Environment.GetFolderPath(Environment.SpecialFolder.desktop)获得桌面
  • 您根本不需要Linq:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string[] directories = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
    
  • System.IO和Linq不是最好的朋友,因为您希望在文件级别处理异常,延迟执行和磁盘访问很困难
  • 使用
    Path
    类创建路径,例如
    Path.Combine(part1,part2,…)
  • 如果只需要路径,则不需要使用
    DirectoryInfo
    类,可以使用静态
    Directory
    File
    方法,如
    Directory.GetDirectories
  • 您可以通过
    Environment.GetFolderPath(Environment.SpecialFolder.desktop)获得桌面
  • 您根本不需要Linq:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string[] directories = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
    

  • 您需要访问哪个文件夹?无法检索数据是什么意思。是否引发异常或发生其他错误?你试过调试你的代码吗?我需要访问桌面上的文件夹。在这里使用LINQ是完全无用的<代码>从工具栏中的foo选择foo
    与简单的
    工具栏
    相同,
    @“C:\Users\”+Environment.UserName+Environment.SpecialFolder.Desktop
    ?这是您需要的路径吗?您需要访问哪个文件夹?无法检索数据是什么意思。是否引发异常或发生其他错误?你试过调试你的代码吗?我需要访问桌面上的文件夹。在这里使用LINQ是完全无用的<代码>从工具栏中的foo选择foo
    与简单的
    工具栏
    相同,
    @“C:\Users\”+Environment.UserName+Environment.SpecialFolder.Desktop
    ?这就是您需要的路径吗?感谢您指出LINQ并不是解决所有问题的最佳解决方案!感谢您指出LINQ并不是解决所有问题的最佳方案!