gpt4 book ai didi

c# - 根据组合框中的选定项目获取文件名

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:38 25 4
gpt4 key购买 nike

我认为这是个大问题......

我有这样的路径..."C:\restore\restoredb\"

在那个路径中我有这样的文件..

 restore-2011-10-12T17-16-51.zip
restore-2011-10-11T13-24-45.zip
restore-2011-05-11T09-45-56.zip
restore-2011-08-11T09-08-07.zip
restore-2010-09-11T09-45-12.zip

我有一个表单,在那个表单中我有一个列表框和组合框(cbrestore)我有这样的组合框项目...月、3 个月、6 个月、年...

我想要的是,如果我选择 combobox item(month) 我想显示在这些日期之间存储在该文件夹中的文件名 (12-10-2011 到12-09-2011)..

如果我选择 combobox item(3 months) 我想显示在这些日期之间存储在该文件夹中的文件名 (12-10-2011 到 12-07-2011 ..在列表框中

为此我已经试过了....

 private void cbrestore_SelectedIndexChanged(object sender, EventArgs e)
{
string fullpathforfiles = @"C:\restore\restoredb\";
string[] allfiles = Directory.GetFiles(fullpathforfiles);
foreach (string single in allfiles)
{
string filenameonly = Path.GetFileName(single);
}
if (cbrestore.SelectedValue == Daterange.type1)
{

}
}
struct Daterange
{
public const string type1 = "Month";
public const string type2 = "3 Months";
public const string type3 = "6 Months";
public const string type4 = "Year";

}

我不知道如何提取该文件名中的确切部分并添加...知道我该怎么做..请..

任何建议和任何代码示例片段对我来说都会非常有用....

非常感谢......

最佳答案

我会这样做:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> t = Directory.GetFiles(@"C:\Users\justin\Desktop\New folder (2)").ToList();
List<String> y = new List<string>();
List<String> u = new List<string>();
foreach (var zzz in t)
{
y.Add(Path.GetFileName(zzz));
}


if (comboBox1.Text == "Month")
{
u =
(from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 1) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
ToList();
}
else if (comboBox1.Text == "3 Month")
{
u =
(from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 3) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
ToList();
}
else if(comboBox1.Text == "1 Year")
{
u =
(from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 12) select s).
ToList();
}

listBox1.DataSource = u;

}

结果是这样的:enter image description here

编辑:修复了您在 SS 中看到的月份选择问题并添加了年份选择。

关于c# - 根据组合框中的选定项目获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7744908/

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