Re: list files in subdirectory trouble!! please look at this snippet
From: Nick Malik [Microsoft] (nickmalik_at_hotmail.nospam.com)
Date: 02/02/05
- Next message: Mattias Sjögren: "Re: readonly behavior for structs"
- Previous message: SerhaD Inanli: "Re: replace"
- In reply to: RML: "list files in subdirectory trouble!! please look at this snippet"
- Next in thread: Champika Nirosh: "Re: list files in subdirectory trouble!! please look at this snippet"
- Reply: Champika Nirosh: "Re: list files in subdirectory trouble!! please look at this snippet"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 1 Feb 2005 22:49:49 -0800
because this line is not good:
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
diNext.GetFiles() will return an array of files that matches the expression.
How do you want .net to create a string that "represents" a reference to an
array?
The default way to do this is to print out the type, which is what you are
getting. If you want to print out the actual contents, then you will need
an inner loop to scan through the array, get the file names, and print them
out.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"RML" <rml@robertlyon.com.au> wrote in message
news:uzBaP4OCFHA.3368@TK2MSFTNGP10.phx.gbl...
> hey guys,
>
> i am looking at this piece of code that lists numbers of files in a
> directory. i want to convert it so it lists the files in th directory that
> end with .doc. i cant seem to get it to output correctly, i have included
> the original code, my modified code, and the output from my modifed code..
> why does it list the files as "System.IO.FileInfo[]"???
>
> thanks
>
> --------------------------------------------------------------------------
------------------------------------
> ORIGINAL C# CODE
> --------------------------------------------------------------------------
------------------------------------
>
> using System;
> using System.IO;
>
> class Test
> {
> public static void Main()
> {
> try
> {
> DirectoryInfo di = new DirectoryInfo(@"c:\");
>
> // Get only subdirectories that contain the letter "p."
> DirectoryInfo[] dirs = di.GetDirectories("*p*");
>
> Console.WriteLine("Number of directories with a p: {0}",
> dirs.Length);
>
> // Count all the files in each subdirectory that contain the
> letter "e."
> foreach (DirectoryInfo diNext in dirs)
> {
> Console.WriteLine("The number of files in {0} with an e is
> {1}", diNext,
> diNext.GetFiles("*e*").Length);
> }
> }
> catch (Exception e)
> {
> Console.WriteLine("The process failed: {0}", e.ToString());
> }
> }
> }
>
> --------------------------------------------------------------------------
------------------------------------
>
> --------------------------------------------------------------------------
------------------------------------
> MY MODIFICATIONS
> --------------------------------------------------------------------------
------------------------------------
> using System;
> using System.IO;
>
> class Test
> {
> public static void Main()
> {
> try
> {
> DirectoryInfo di = new DirectoryInfo(@"h:\");
>
> // Get Subdirectories
> DirectoryInfo[] dirs = di.GetDirectories();
>
> Console.WriteLine("Number of directories is {0}",
dirs.Length);
>
> // Count all the files in each subdirectory that contain the
> letter "e."
> foreach (DirectoryInfo diNext in dirs)
> {
> Console.WriteLine(diNext.GetFiles("*.doc"));
> }
> }
> catch (Exception e)
> {
> Console.WriteLine("The process failed: {0}", e.ToString());
> }
> }
> }
>
>
> --------------------------------------------------------------------------
------------------------------------
>
> --------------------------------------------------------------------------
------------------------------------
> THE OUTPUT
> --------------------------------------------------------------------------
------------------------------------
> Number of directories is 4
> System.IO.FileInfo[]
> System.IO.FileInfo[]
> System.IO.FileInfo[]
> System.IO.FileInfo[]
> --------------------------------------------------------------------------
------------------------------------
>
>
- Next message: Mattias Sjögren: "Re: readonly behavior for structs"
- Previous message: SerhaD Inanli: "Re: replace"
- In reply to: RML: "list files in subdirectory trouble!! please look at this snippet"
- Next in thread: Champika Nirosh: "Re: list files in subdirectory trouble!! please look at this snippet"
- Reply: Champika Nirosh: "Re: list files in subdirectory trouble!! please look at this snippet"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|