gpt4 book ai didi

c# - LINQ:来自 string[] 的字典

转载 作者:行者123 更新时间:2023-11-30 14:52:25 25 4
gpt4 key购买 nike

我必须根据在 string[] 中检索到的绝对路径创建一个用于 DropDownListFor 的字典。我想知道是否有办法让键只是文件夹名称而值是完整路径?

这是我目前所拥有的:

// read available content folders from FTP upload root
var path = HttpContext.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ResearchArticleFTPUploadRoot"]);
var subfolders = System.IO.Directory.GetDirectories(path).ToDictionary(r => r, v => v);
ViewBag.BodyFolderChoices = new SelectList(subfolders, "Key", "Key");

我试过:

var subfolders = System.IO.Directory.GetDirectories(path).ToDictionary(r => r, v => new { v.Substring(v.LastIndexOf("/"), v.Length-v.LastIndexOf("/"))});

认为在文件夹名称路径中的最后一个“/”之后抓取作为键...不起作用...想法?

最佳答案

using System.IO; // to avoid quoting the namespace everywhere it's used

var subfolderPaths = Directory.GetDirectories(path);
var dictionary = subfolderPath.ToDictionary(p => Path.GetFileName(p), p => p);

请注意,如果您为其提供文件夹的完整路径,此上下文中的 GetFileName 将返回文件夹名称。

关于c# - LINQ:来自 string[] 的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31725230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com