Adding to arrays of strings

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
I'm trying to create something to Zip up all files within a directory and its' sub directory using SharpZipLib.

Simply put, you type the name of a directory into a text box and its passed into the creation of an array like so:

Visual Basic:
Dim strFilesToCompress() as string = IO.Directory.GetFiles(inputbox.text)

That gets all of the files in a single directory and throws them into a string array.

This string array is passed to another procedure which actually does the zipping based on the paths and filenames in this array.

Now to add subdirectories, it should be as simple as creating a recursive procedure to get all the directories and add their files to the strFilesToCompress() array.

Unfortunately I don't know my array technique in this way very well and the code:

Visual Basic:
        For Each directory In IO.Directory.GetDirectories(SourceDirectory)
            strFilesToCompress() += IO.Directory.GetFiles(directory)
            zipDirectory(directory) 'The recursive part
        Next

Gives an error. Apparently you can only do this when first Dimming it?

what syntax would I use to keep adding more files after its first created?

Would I have to break down and use a For...Next loop to assign each filepath a specific spot in the array?
 
No, forget about the string array. Use an ArrayList instead. Or create your own List class. It is easier just to use ArrayList though.

C#:
/// <summary>
		/// Returns a string array of all files in a directory.
		/// </summary>
		public ArrayList GetAllFiles(string Dir)
		{
			string[] E= Directory.GetFileSystemEntries(Dir);
			
			for(int j=0; j<E.Length; j++)
			{
				if(Directory.Exists(E[j]))
					GetAllFiles(E[j]);
				else
					RecList.Add(E[j]);
			}
			return RecList;
		}
RecList is the ArrayList.
 
Well I managed to Jigger Rig it... Its pretty sad but it works. I'd like to use a more efficient method.

Visual Basic:
    Public astrFileNames(10000) As String

    Public Sub zipWholeDirectory(ByVal SourceDirectory As String)
        Dim directory As String
        Dim File As String
        Dim iCounter As Integer = 0

        For Each directory In IO.Directory.GetDirectories(SourceDirectory)
            For Each File In IO.Directory.GetFiles(directory)
                astrFileNames(iCounter) = File
                iCounter += 1
            Next
        Next
    End Sub

Then I have to test my zipping procedure for a blank line (which would cause an error) and tell it that when it got a blank line, its over.

If I were to use this hodgepodge method, I'd realisitically have to create another sub, just to count out how many files there are, just so I can dim my array to the right length.
 
aewarnick said:
No, forget about the string array. Use an ArrayList instead. Or create your own List class. It is easier just to use ArrayList though.

>>snip<<

RecList is the ArrayList.

Oh, my god. C# really does remind me of Java.

I'm getting flashbacks of my java class now... Actually Java was pretty cool, our IDE was crap though. I've heard that there were better (JCreator).

If C# looks this much like Java, then what is J#?

Well your method looks better to me, less 'patched'.

I'm going to translate this to VB and try this :)

>>Edit<<

Whoops, Forgot my manors :p
Thank you very much for this code :)
 
Ok, I gave converting it a try. It looks very good with one problem. What is RecList? did you forget to dim it?

Well this is what I got out of converting it:

Visual Basic:
    Public Function GetAllFiles(ByVal Dir As String) As ArrayList
        Dim e() As String = Directory.GetFileSystemEntries(Dir)
        Dim iCounter As Integer

        For iCounter = 0 To e.Length
            If Directory.Exists(e(iCounter)) Then
                GetAllFiles(e(iCounter))
            Else
                reclist.Add(e(iCounter))
            End If
        Next
        Return reclist
    End Function

RecList is the error I got.

That was some pretty crunchy stuff you threw down. I've never used Directory.Exists or Directory.GetFileSystemEntries before, they look more useful.

Any help with the RecList? Is it another array?
 
Back
Top