Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Csharp.net
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 13-Mar-13 19:38pm
Rate this: bad
good
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...
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

    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();
        }
 

    }
  Permalink  
v4
Comments
Member 9326300 at 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 at 14-Mar-13 5:29am
   
Updated solution with dynamically adding textbox and also aligning its width...
Member 9326300 at 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 at 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
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
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