Page 1 of 1

Adding filenames to an array

Posted: 02 Dec 2005, 16:03
by Mechano
I have to create an application that sorts a list of pdf files from oldest to newest.

I can retrieve the info from one file easily, but how will I retrieve it from all the files in a specific directory, so that I can add their LastWriteTimeUTC property to an arraylist so I can sort them??? 8O

Posted: 02 Dec 2005, 16:14
by rustypup
language?

do you have access to some form of Collection? if not you will need to:
1) Acquire the total number of files
2) Remove those without .pdf extension
3) initialise an array to the desired length
4) acquire an index into the array
5) loop through the files, adding each into the newly initialised array..

when you get this far we can discuss sorting algorithms.. bubble sort anything below 50 only... thereafter your trade-off in using a quick or quick+insertion sort combination is the better option...

language?

Posted: 02 Dec 2005, 16:26
by Mechano
C#.Net in Visual Studio 2003

Posted: 02 Dec 2005, 16:34
by rustypup
then you already have access to dynamic, typed, collections, as well as all the static sort methods that go with them...

what have you done so far?

Posted: 05 Dec 2005, 08:16
by Mechano
I don't know how this is gonna look...
It gives me an error : The given path's format is not supported


string filePath = "C:/pdf/";
ArrayList fileLst = new ArrayList();

Directory fDir;
string [] fFile = Directory.GetFiles(filePath);
int x = 0;
foreach (string DirFile in fFile)
{
FileInfo infoF = new FileInfo(filePath + DirFile.ToString() + ".pdf");
fileLst.Add(infoF.LastWriteTimeUtc);
fileLstBox.Items.Add(x.ToString()+ ". " + fileLst[x]);
x++;
}

MessageBox.Show("Time :" + fileLst[0] + " Time 2 : " + fileLst[1]);

Posted: 05 Dec 2005, 11:45
by HdB
Hi, try this :)

private void Form1_Load(object sender, System.EventArgs e)
{
DirectoryInfo theDir = new DirectoryInfo("c:\\pdf");
FileInfo[] theFiles = theDir.GetFiles();

int intCount = 0;
if (theFiles.Length > 0)
{
// Do sorting
if (theFiles.Length > 1)
{
FileInfo tmpFile;
int intPass = (theFiles.Length - 1);
bool blnDone = true;

while (intPass >= 0 & blnDone == false)
{
blnDone = true;
for(intCount = 0; intCount <= (intPass - 1); intCount++)
{
if (theFiles[intCount].LastWriteTimeUtc > theFiles[intCount + 1].LastWriteTimeUtc)
{
blnDone = false;
tmpFile = theFiles[intCount + 1];
theFiles[intCount + 1] = theFiles[intCount];
theFiles[intCount] = tmpFile;
}
}
intPass -= 1;
}
}

//Show files
for(intCount = 0; intCount <= (theFiles.Length - 1); intCount++)
{
fileLstBox.Items.Add(intCount.ToString() + " - " + theFiles[intCount].FullName + " : " + theFiles[intCount].LastWriteTimeUtc);
}
}
}

Edit: crap! stupid indenting

Posted: 06 Dec 2005, 09:25
by Mechano
never mind the indenting...you helped me a lot

the FileInfo[] part was what I needed

Tx :D

Another Question

Posted: 06 Dec 2005, 11:16
by Mechano
I have to forms for this application --- mainForm and configForm

I do not want the configForm to appear in the windows taksbar when I activate it from within the mainForm.

I also want the app to minimize to the system tray when I click minimize

Any help....I'm busy googling

Posted: 06 Dec 2005, 11:42
by HdB
You'll need a NotifyIcon, and a Context menu. Declared in code, in your case.

Posted: 07 Dec 2005, 16:56
by Mechano
Right, this app is close to finished...

I want to write the contents of a richTextBox to a text file, in exactly the same context as I view it in my app. In other words, where there is a line break, I want it to to be shown in the text file as well

I use this,
StreamWriter wLog = new StreamWriter({file details});
wLog.WriteLine(detailsTxtBox.Text);
wLog.Flush();
wLog.Close();

however, this writes all the lines in my textbox side by side, with a '□' showing where the line breaks should be...any ideas...

Posted: 07 Dec 2005, 17:01
by Law
what are you opeming the file with?

Posted: 07 Dec 2005, 17:04
by Mechano
Mechano wrote:
I want to write the contents of a richTextBox to a text file.....
Well, seeing that it's a textfile, with a *.log extension, i'd like to open it with notepad...

Shoul I rather just use a multiline Textbox instead of a richTextBox???

Posted: 07 Dec 2005, 17:12
by Law
notepad does not support some rich text. try opening it in wordpad and see what happens??