Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据组合框中的选定项获取文件名_C#_.net_Winforms_String_File - Fatal编程技术网

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

C# 根据组合框中的选定项获取文件名,c#,.net,winforms,string,file,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 我有一个表单,在这个表单中我有一个列表框和组合框(

我认为这是一个大问题

我有这样的路径…“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个月,年

我想要的是,如果我选择
组合框项目(月)
,我想显示这些日期之间存储在该文件夹中的文件名
(2011年10月12日至2011年9月12日)

如果我选择组合框项目(3个月)我想在列表框中显示这些日期之间存储在该文件夹中的文件名
(2011年10月12日至2011年7月12日)

为此,我试过这个

 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";  

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

任何建议和任何代码示例片段都将对我非常有用


非常感谢….

我想您可以使用下面的内容从字符串中提取日期

    filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") - filenameonly.IndexOf("-") - 1)

希望这有帮助

我想您可以使用下面的方法从字符串中提取日期

    filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") - filenameonly.IndexOf("-") - 1)

希望这有帮助

使用Praveen的代码

filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") -     filenameonly.IndexOf("-") - 1)
然后将其拆分为一个数组
filename only.split(“-”)

重新设置它们的范围,以便您可以转换为日期,并检查它是否在3个月内 DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months } DateTime filetime=newdatetime(); parse(filenameonlyarray[2]+“/”+filenameonlyarray[1]+“/”+filenameonlyarray[0]); if(filetime.compareto(DateTime.Now.AddMonths(-3)>0) { //3个月内 DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months } }


使用此日期对象,您现在可以使用它来检查它是否在“”的3个月内。

使用Praveen的代码

filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") -     filenameonly.IndexOf("-") - 1)
DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months } 然后将其拆分为一个数组
filename only.split(“-”)

重新设置它们的范围,以便您可以转换为日期,并检查它是否在3个月内 DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months } DateTime filetime=newdatetime(); parse(filenameonlyarray[2]+“/”+filenameonlyarray[1]+“/”+filenameonlyarray[0]); if(filetime.compareto(DateTime.Now.AddMonths(-3)>0) { //3个月内 DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months } }

使用此日期对象,您现在可以使用它来检查它是否在“”的3个月内。

我会这样做:

DateTime filetime = New DateTime(); filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]); if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0) { //within 3 months }
    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;

    }
private void组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
List t=Directory.GetFiles(@“C:\Users\justin\Desktop\newfolder(2)”).ToList();
列表y=新列表();
列表u=新列表();
foreach(t中的变量zzz)
{
y、 添加(Path.GetFileName(zzz));
}
如果(comboBox1.Text==“月”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<1)和&(DateTime.Now.Year-DateTime.Parse(s.Substring(8,10)).Year==0)选择s)。
托利斯特();
}
else if(comboBox1.Text==“3个月”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<3)和&(DateTime.Now.Year-DateTime.Parse(s.Substring(8,10)).Year==0)选择s)。
托利斯特();
}
else if(comboBox1.Text==“1年”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<12)选择s)。
托利斯特();
}
listBox1.DataSource=u;
}
结果是:

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

我会这样做:

    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;

    }
private void组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
List t=Directory.GetFiles(@“C:\Users\justin\Desktop\newfolder(2)”).ToList();
列表y=新列表();
列表u=新列表();
foreach(t中的变量zzz)
{
y、 添加(Path.GetFileName(zzz));
}
如果(comboBox1.Text==“月”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<1)和&(DateTime.Now.Year-DateTime.Parse(s.Substring(8,10)).Year==0)选择s)。
托利斯特();
}
else if(comboBox1.Text==“3个月”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<3)和&(DateTime.Now.Year-DateTime.Parse(s.Substring(8,10)).Year==0)选择s)。
托利斯特();
}
else if(comboBox1.Text==“1年”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-DateTime.Parse(s.Substring(8,10)).Month)<12)选择s)。
托利斯特();
}
listBox1.DataSource=u;
}
结果是:


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

Wow.非常感谢KreepN,它工作得很好…..如果你不介意的话,我在combobox中有另一个选项,比如一年,我可以怎么做?请你解释一下…..是的,我会用我为mont添加的一个小修复,在一小段时间内为你呕吐和编辑h chunks,因为如果你看屏幕截图,你会看到3个月从2010年开始上市。我即将过渡回家,所以可能需要一个小的等待。哇……非常感谢KreepN,它工作正常……如果你不介意再问一个问题,我在combobox中有另一个选项,比如一年,我该怎么做?请你解释一下……是的,我会扔我为这个月添加了一个小补丁,可以帮你在一点小范围内进行编辑,因为如果你看屏幕截图,你会发现这3个月从2010年开始上市。我即将过渡回家,所以这可能是一个小的等待。