Click here to Skip to main content
11,935,053 members (54,632 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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
FileInfo file = new FileInfo(@"C:\ITEM1");
DateTime updatedTime = file.LastWriteTime;
textBox1.Text = "ITEM1: " + updatedTime;
//no, file does not exists
Posted 13-Mar-13 19:38pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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...

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

public partial class Form1 : Form
    int maxwidth = 0;
    public Form1()
    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
        //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();
                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;
    private void AlignWidth()
        foreach(Control ctrl in this.Controls.OfType<TextBox>())
            ctrl.Width = maxwidth;
    private void Form1_Load(object sender, EventArgs e)

Member 9326300 14-Mar-13 1:36am
Thank you very much Kuthuparakkal, for your prompt reply.
But I would like each filterd file in different textbox, like textbox1, 2, 3, etc.
Thanks anyway.
Kuthuparakkal 14-Mar-13 5:29am
Updated solution with dynamically adding textbox and also aligning its width...
Member 9326300 14-Mar-13 23:41pm
Thanks Kuthuparakkal for this great solution.
But what I want is as simply as retrieving 2 filenames from a folder, than put their lastwritetime in each Textbox(textbox1 and textbox2) already greated for that information.
Kuthuparakkal 14-Mar-13 23:53pm
if it helped, plz mark answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100