C# 使用LINQ访问文件夹
我想使用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
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();
Path
类创建路径,例如Path.Combine(part1,part2,…)
DirectoryInfo
类,可以使用静态Directory
或File
方法,如Directory.GetDirectories
Environment.GetFolderPath(Environment.SpecialFolder.desktop)获得桌面代码>
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string[] directories = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
Path
类创建路径,例如Path.Combine(part1,part2,…)
DirectoryInfo
类,可以使用静态Directory
或File
方法,如Directory.GetDirectories
Environment.GetFolderPath(Environment.SpecialFolder.desktop)获得桌面代码>
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并不是解决所有问题的最佳方案!