Hey, I'm a fairly new programmer and currently working on a University assignment and theres an extra feature I want to implement but can't seem to work it out
I have these two lines at the start of my program
DirectoryInfo folderInfo = new DirectoryInfo(C:\\Windows);
FileInfo[] files = folderInfo.GetFiles();
but I've been trying to create a new FileInfo array that will only contain files from DirectoryInfo if the file size is within a certain range of a user inputted value
Someone recommended I use a where command which I haven't been taught yet, and searching online hasn't helped much and I just ended up with a line like
List<FileInfo> sizeFiltered = new List<FileInfo>();
foreach(FileInfo f in files)
{
if( f.Length >= lowerBound && f.Length >= upperBound )
sizeFiltered.Append(f);
}
Probably though your compiler is angry because Where is returning an IEnumerable<FileInfo>, not a FileInfo[] (??). You can probably call .ToArray() (or maybe .ToArray<FileInfo>() -- not sure) and get your Linq query to work. Elsewise, post the error message.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Okay my compiler isn't angry any more but I'm having a bug when trying to display the contents of the list
I'm trying to receive an input number from the user and then displaying information about files that are within 1 mb of that chosen size. My code is below and it runs, but when I try entering a value of 1, I receive some results where the size is greater than 1 mb difference. if anyone can spot the reason for this in my code it would be greatly appreciated
Code:
int mb = 1048576;
int userSize = 0;
bool sizeIsNumber = int.TryParse(Console.ReadLine(), out userSize);
while (sizeIsNumber == false || userSize < 0)
{
Console.Clear();
Console.WriteLine("That is an invalid selection, please enter a whole number greater than 0");
sizeIsNumber = int.TryParse(Console.ReadLine(), out userSize);
}
int lowerBound = userSize - mb;
int upperBound = userSize + mb;
if (lowerBound < 0)
{
lowerBound = 0;
}
List<FileInfo> sizeFiltered = new List<FileInfo>();
foreach (FileInfo f in fileArray)
{
if (f.Length >= lowerBound && f.Length <= upperBound)
{
sizeFiltered.Add(f);
}
}
for (int i = 0; i < sizeFiltered.Count; i++)
{
Console.Write("{0}. ", i + 1);
Console.Write(fileArray[i].Name);
Console.Write(" ({0})", fileArray[i].Length);
Console.WriteLine();
}
Last edited by BioPhysEngr; December 22nd, 2012 at 11:31 AM.
Reason: added code and /code tags
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
An easy mistake to make! As I learned when programming in assembly (for which there was no debugger I liked): code review - just re-reading through your own code - is the single most powerful debugging tool you have at your disposal!
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks