|
-
Aug 17th, 2007, 10:45 PM
#1
Thread Starter
PowerPoster
[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 sender, EventArgs 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(path, scan);
for (int i = 0; i < theFiles.Length - 1; i++)
{
MessageBox.Show(theFiles[i]);
}//end for
}//end button1_Click
public string[] GetFilesAfterDate(string folder, DateTime date)
{
FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
List<FileInfo> fileList = new List<FileInfo>();
if (files.Length > 0)
{
for (int index = 0; index < 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 = 0; index < 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.

-
Aug 17th, 2007, 11:24 PM
#2
Re: Array problem
I'd say that this is your problem:
C# Code:
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:
for (int index = 0; index <= files.GetUpperBound(0); index++)
or, preferably:
C# Code:
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
-
Aug 17th, 2007, 11:41 PM
#3
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();
}
-
Aug 17th, 2007, 11:43 PM
#4
Re: Array problem
 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.
-
Aug 17th, 2007, 11:45 PM
#5
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.
-
Aug 18th, 2007, 12:08 PM
#6
Thread Starter
PowerPoster
Re: Array problem
Thanks for the input guys. I am having an issue with the code though
PHP Code:
public string[] GetFilesAfterDate(string folder, DateTime date)
{
FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.env");
List<string> fileList = new List<string>();
foreach (FileInfo f in files)
{
if (f.LastWriteTime >= date) fileList.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.

-
Aug 18th, 2007, 12:11 PM
#7
Re: Array problem
Whoops
Code:
fileList.Add(f.Name);
-
Aug 18th, 2007, 12:13 PM
#8
Thread Starter
PowerPoster
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.

-
Aug 18th, 2007, 12:18 PM
#9
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|