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

标签 c# .net winforms string file

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

我有这样的路径..."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/

相关文章:

C#抽象类说明

c# - 为什么 Silverlight TextBox 使用\r 而不是 Environment.Newline (\r\n) 作为换行符?

.net - 枚举错误提供程序中的错误

C# WinForms MDI 问题

c# - 是否有 "stock"空闲/等待对话框?

c# - 在 HtmlHelper 扩展方法中使用匿名对象

c# - BouncyCaSTLe 与 iTextSharp 冲突

c# - 如何从所选目录加载少量文件 ASP.NET

.net - 有人知道 .Net 的 Atom APP 服务器吗?

c# - 如何使用 Simple Injector 注册 Windows 窗体