Click here to Skip to main content
15,896,383 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted

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
 
Share this answer
 
C#
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();
    }


}
 
Share this answer
 
v4
Comments
[no name] 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...
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900