Results 1 to 9 of 9

Thread: [RESOLVED] Array problem

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Resolved [RESOLVED] Array problem

    Hey guys, I have a method that gets all the file names in a folder that have a certain extension..such as ".env" or whatever. It puts those names in an array which is returned thru the method. I then loop thru this array of file names.

    The problem is, if there are only one or two files in the folder it returns nothing. If there are three files it returns one, if there are 4 files it returns two etc..

    It seems to not be giving me a correct count.

    Here is the code, any help is appreciated!

    PHP Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace 
    WindowsApplication2
    {
        public 
    partial class Form1 Form
        
    {
            public 
    Form1()
            {
                
    InitializeComponent();
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                        
    string[] theFiles;
                        
    theFiles = new string[] { };
                        
    string lastScan "3/21/2003 11:12:23 AM";
                        
    string path "C:\\test";
                        
    DateTime scan Convert.ToDateTime(lastScan);
                        
    theFiles GetFilesAfterDate(pathscan);

                        for (
    int i 0theFiles.Length 1i++)
                        {
                            
    MessageBox.Show(theFiles[i]);
                        }
    //end for

            
    }//end button1_Click

            
    public string[] GetFilesAfterDate(string folderDateTime date)
            {
                
    FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
                List<
    FileInfofileList = new List<FileInfo>();

                if (
    files.Length 0)
                {
                    for (
    int index 0index files.GetUpperBound(0); index++)
                    {
                        if (
    files[index].LastWriteTime >= date)
                        {
                            
    fileList.Add(files[index]);
                        }
    //end if
                    
    }//end for
                
    }//end if

                
    string[] filePaths = new string[fileList.Count];

                for (
    int index 0index filePaths.GetUpperBound(0); index++)
                {
                    
    filePaths[index] = fileList[index].Name;
                }
    //end for

                
    return filePaths;
            }
    //end GetFilesAfterDate
        
    }

    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array problem

    I'd say that this is your problem:
    C# Code:
    1. for (int index = 0; index < files.GetUpperBound(0); index++)
    I think you've let some VB thinking slip in there. That should either be:
    C# Code:
    1. for (int index = 0; index <= files.GetUpperBound(0); index++)
    or, preferably:
    C# Code:
    1. for (int index = 0; index < files.Length; index++)
    I would also simplify your function somewhat:
    Code:
    public string[] GetFilesAfterDate(string folder, DateTime date)
    {
        FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
        List<string> fileList = new List<string>();
    
        if (files.Length > 0)
        {
            for (int index = 0; index < files.Length; index++)
            {
                if (files[index].LastWriteTime >= date)
                {
                    fileList.Add(files[index].Name);
                }//end if
            }//end for
        }//end if
    
        return fileList.ToArray();
    }//end GetFilesAfterDate
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Array problem

    You should use foreach, really, unless you need to modify the array/collection during enumeration, which you don't.

    Code:
    public string[] GetFilesAfterDate(string folder, DateTime date)
    {
        FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
        List<string> fileList = new List<string>();
    
        if (files.Length > 0)
            foreach (FileInfo f in files)
                if (f.LastWriteTime >= date)
                    fileList.Add(f);
    
        return fileList.ToArray();
    }

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array problem

    Quote Originally Posted by penagate
    You should use foreach, really, unless you need to modify the array/collection during enumeration, which you don't.

    Code:
    public string[] GetFilesAfterDate(string folder, DateTime date)
    {
        FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
        List<string> fileList = new List<string>();
    
        if (files.Length > 0)
            foreach (FileInfo f in files)
                if (f.LastWriteTime >= date)
                    fileList.Add(f);
    
        return fileList.ToArray();
    }
    I concur.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Array problem

    In fact you could further simplify that by removing the if (files.Length > 0) check, since neither a for nor foreach loop will enter anyway.

  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Array problem

    Thanks for the input guys. I am having an issue with the code though

    PHP Code:
    public string[] GetFilesAfterDate(string folderDateTime date)
            {
                
    FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
                List<
    stringfileList = new List<string>();


                foreach (
    FileInfo f in files)
                {
                    if (
    f.LastWriteTime >= datefileList.Add(f);
                }
    //end for

                
    return fileList.ToArray();
            }
    //end GetFilesAfterDate 
    fileList.Add(f) gives an error: "The best overloaded method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments"

    Not sure what it is expecting there.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Array problem

    Whoops

    Code:
    fileList.Add(f.Name);

  8. #8

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Array problem

    I figured that out...was supposed to be fileList.Add(f.ToString());
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  9. #9

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Array problem

    Aight cool guys...after som emore testing it seems to work perfectly and is also a lot more optimized.

    Thanks a lot!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width