查找第一个子目录中的所有文件

时间:2015-09-29 19:59:11

标签: c# path subdirectory

我有一个应用程序,可以在require "sinatra" require "instagram" enable :sessions CALLBACK_URL = "http://localhost:4567/oauth/callback" Instagram.configure do |config| config.client_id = "YOUR_CLIENT_ID" config.client_secret = "YOUR_CLIENT_SECRET" # For secured endpoints only #config.client_ips = '<Comma separated list of IPs>' end get "/" do '<a href="/oauth/connect">Connect with Instagram</a>' end get "/oauth/connect" do redirect Instagram.authorize_url(:redirect_uri => CALLBACK_URL) end get "/oauth/callback" do response = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL) session[:access_token] = response.access_token redirect "/nav" end 后面的所有目录中搜索,如下所示:

SELECT *
FROM
(SELECT 
[TransactionNumber]
,MIN(CASE WHEN [Entry] = 'Updated status to Awaiting OCLC Sending' THEN [DateTime] END) AS [start] 
,MAX(CASE WHEN [Entry] = 'Updated Status to: Delivered to Web' THEN [DateTime] END) AS [end] 
FROM [ILLData].[dbo].[History]
GROUP BY [TransactionNumber]) x
WHERE [start] IS NOT NULL 
AND [end] IS NOT NULL 

这很好但现在我只想在这个文件夹的第一个子目录中找到所有应用程序。像这样:

Documents/GameLauncher/

我已经四处寻找并想出了这个:

var foundApplications = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url"));

有没有人有任何提示可以解决这个问题,甚至是在GameLauncher文件夹的第一个子目录中找到这些文件的更好方法?

感谢阅读/帮助。

3 个答案:

答案 0 :(得分:1)

不要使用&#34; all&#34;选项,如果你不想要所有的,那么简单。

var path = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    @"GameLauncher");
var includedExtensions = new HashSet<string> { ".exe", ".lnk", ".url" };
var files =
    from dir in Directory.EnumerateDirectories(path)
    from file in Directory.EnumerateFiles(dir)
    let extension = Path.GetExtension(file)
    where includedExtensions.Contains(extension)
    select file;

答案 1 :(得分:0)

您应该使用列表而不是IEnumerable,因为它会动态增长。

var foundApplications = new List<string>();

var folders = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher");

//Use folders to find applications and add them to foundApplications
for (int i = 0; i < folders.Count(); i++)
{
    foundApplications.AddRange(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher/" + folders[i], "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url").ToList());
}

foreach (var application in foundApplications){
    MessageBox.Show(application.ToString());
}

答案 2 :(得分:0)

如果您想将一个IEnumerable附加到另一个Concat,则需要使用foundApplications。您还必须将IEnumerable初始化为空var folderPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "GameLauncher"); var folders = Directory.GetDirectories(folderPath); IEnumerable<string> foundApplications = Enumerable<string>.Empty; //Use folders to find applications and add them to foundApplications foreach(var subFolder in folders) { string path = Path.Combine(folderPath, subFolder); foundApplications.Concat( Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly) .Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url"))); } foreach (var application in foundApplications){ MessageBox.Show(application.ToString()); }

SearchOption.TopDirectoryOnly

此外,我确定您要使用SearchOption.AllDirectories而不是ViewController