Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,069 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,601 people online right now. Registration is fast and FREE... Join Now!




System.IO.Directory

 
Reply to this topicStart new topic

System.IO.Directory

nbarten
4 Apr, 2008 - 08:13 AM
Post #1

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
hi all.

Well, i wanted to use the System.IO.Directory library because i want to get all the files in a certain directory...

The problem is, when i type in Visual Studio 2008 at the first lines 'System.IO.' then i see only four options, but not Directory with them. So i decided to type it anyway, but it doesn't work, it just can't find the library i think.

I tried it also in Visual C# 2005 Express Edition, but with no luck. I have .NET Framework 3.5 installed (also downloaded it again and 'repaired' it, but still with no luck at all).

So i hope you guys can help.

Thanx
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: System.IO.Directory
4 Apr, 2008 - 08:51 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Directory is an object of the System.IO namespace. So you can't specify it as if it was a separate namespace under the System.IO namespace.

At the top of the file just specify System.IO; nothing more. Then in your code you can type "Directory" and you will see the object there to use. You only use the dot notation at the top to include namespaces. Since the Directory object is in the System.IO namespace itself, you just include System.IO.

That is all you need. smile.gif
User is offlineProfile CardPM
+Quote Post

nbarten
RE: System.IO.Directory
4 Apr, 2008 - 09:58 AM
Post #3

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
ah ok, thanks.

But then i still have a problem i don't understand. This is my little test code:

CODE
namespace xmltest
{
    class Program
    {
        Directory mapDir = new Directory("Maps");

        static void Main(string[] args)
        {
            
            //mapDir = new Directory("Maps");
            File[] files = mapDir.GetFiles();
            foreach (File f in files)
            {
                Console.WriteLine("{0}", f.FullName);
            }


Just a part of it of course. Now, it gives me this error:

'Error 1 Cannot declare a variable of static type 'System.IO.Directory'

Thanks for your help.

This post has been edited by nbarten: 4 Apr, 2008 - 09:59 AM
User is offlineProfile CardPM
+Quote Post

zakary
RE: System.IO.Directory
4 Apr, 2008 - 10:26 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
change to
static Directory mapDir = new Directory("Maps");


User is offlineProfile CardPM
+Quote Post

nbarten
RE: System.IO.Directory
4 Apr, 2008 - 10:29 AM
Post #5

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
don't work:

'Error 1 Cannot declare a variable of static type 'System.IO.Directory'

Very weird (well i think), because the Directory object isn't made in a static class?

This post has been edited by nbarten: 4 Apr, 2008 - 10:33 AM
User is offlineProfile CardPM
+Quote Post

nbarten
RE: System.IO.Directory
4 Apr, 2008 - 10:43 AM
Post #6

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
Solved my problem, my C# book is a bit outdated i think.

This will work:

CODE
DirectoryInfo mapDir = new DirectoryInfo("Maps");
            FileInfo[] files = mapDir.GetFiles();
            
            foreach (FileInfo f in files)
            {
                Console.WriteLine("{0}", f.FullName);
            }


Thanks for all the help anyway. icon_up.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: System.IO.Directory
4 Apr, 2008 - 12:05 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
csharp


String files[] = Directory.GetFiles("c:\mydirectory");




Its an object, a static object meaning you don't create an instance of it. You just use the methods and variables associated with it. DirectoryInfo will work but keep what I have said above in mind because you will need it in the future.

wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:58PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month