FileInfo c#在文件夹中找到2个文件 [英] FileInfo c# find 2 files in folder

查看:70
本文介绍了FileInfo c#在文件夹中找到2个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我写下面的代码是为了检查文件夹中存在的文件(csharp),

它有效但我的问题是:

如何以同样的方式检查2个文件或文件数组?

提前谢谢

-------代码

FileInfo file = new FileInfo(@C:\ ITEM1);

if(file.Exists)

{

DateTime updatedTime = file.LastWriteTime ;

textBox1.Text =ITEM1:+ updatedTime;

}

else

{

//否,文件不存在

Hi all, I wrote the code below in order to check presence of file in a folder (csharp),
It works but my question is:
How to check 2 files or array of files by the same way?
Thanks in advance
-------code
FileInfo file = new FileInfo(@"C:\ITEM1");
if(file.Exists)
{
DateTime updatedTime = file.LastWriteTime;
textBox1.Text = "ITEM1: " + updatedTime;
}
else
{
//no, file does not exists

推荐答案

您可以循环查看所需数量的文件。



你在支票上做什么取决于你想要什么。您可能想要检查1)所有文件是否存在,2)至少有一个文件存在,3)所有文件丢失,4)至少缺少一个文件等等...



-SA
You can check as many files as you need, in a loop.

What you do on the check depends on what you want. You may want to check if 1) all files present, 2) at least one file presents, 3) all files are missing, 4) at least one file is missing, etc...

—SA


public partial class Form1 : Form
{
    int maxwidth = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void SearchAndDisplay()
    {
        Control ctrl = null; ;
        int relative = 10;
        DirectoryInfo di = new DirectoryInfo(@"C:\Users\kuthupar\Documents");
        // An empty list of FileInfo objects
        List<FileInfo> lFi = new List<FileInfo>();

        //Add the files of the directory to the list
        lFi.AddRange(di.GetFiles("BET*"));

        //Find the files on the list using a delegate
        lFi = lFi.FindAll(delegate(FileInfo f) { return f.Extension.ToLower() == ".ppt" || f.Extension.ToLower() == ".pptx" || f.Extension.ToLower() == ".ppt"; });
        int i = 1;
        //Iterate over each filtered file
        foreach (FileInfo fi in lFi)
        {
            TextBox txtbx = new TextBox();
            txtbx.Name = "TextBox " + i.ToString();
            try
            {
                Control[] ctrlArray = this.Controls.Find("TextBox " + (this.Controls.OfType<TextBox>().Count()).ToString(), false);
                ctrl = ctrlArray[0];
            }
            catch { }
            if (ctrl != null)
            {
                relative = ctrl.Bottom + ctrl.Height / 2;
            }
            txtbx.Top = relative;
            txtbx.Text = fi.Name + ":" + fi.LastWriteTime.ToString();
            maxwidth = maxwidth < txtbx.Text.Length * 6 + 10? txtbx.Text.Length*6+10: maxwidth;
            txtbx.Width = maxwidth;
            this.SuspendLayout();
            this.Controls.Add(txtbx);
            this.ResumeLayout();
            i++;
        }

    }

    private void AlignWidth()
    {
        foreach(Control ctrl in this.Controls.OfType<TextBox>())
        {
            ctrl.Width = maxwidth;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SearchAndDisplay();
        AlignWidth();
    }


}


这篇关于FileInfo c#在文件夹中找到2个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆