Changing directory

I am attempting to browse through the file system of a usb drive, I have scanned the Ebook but failed to locate any examples on how to display a directory other than the root directory.

string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);

I was thinking it would be something simple like e.Volume.NewDirectory but am completely lost

Regards,
Aaron

Directory.CreateDirectory(path);

Not sure what you mean by “display”, judging by your example I think you mean how to make a new folder.

EDIT: If you mean get files from a sub-folder, then:

Directory.GetDirectories(e.Volume.RootDirectory + “\MyFolder\”);

Sorry I should have explained better.

My project currently loads and displays the root directory onto a screen, the user can than use up and down buttons to select a file or a directory. If the user selects a directory I wish to “enter” that directory so i can display the selected directories contents onto the LCD.

it doesn’t work like DOS, you’ll have to get the name of the folder the user wants to enter, then use Directory.GetDirectories(Root + SelectedDirectory);

Use System.IO.Directory.SetCurrentDirectory.

An easy example for opening sub directories…


string sFolder = "WhateverNewFolderYouSelected";
System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory + "\\" + sFolder);

For going up one level…


System.IO.Directory.SetCurrentDirectory("..");

im having problems visualizing how to use the given command. This is how I tried to apply it:

//Testing changing the directory
                string sFolder = "AARON";
                //System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory + "\\" + sFolder);
                System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory + "\\" + sFolder);
 

                string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);

I get an error from the line “System.IO.Directory.GetCurrentDirectory + “\”” which is:

Error 1 Operator ‘+’ cannot be applied to operands of type ‘method group’ and ‘string’ C:\Users\Aaron\documents\visual studio 2010\Projects\Program.cs 166 57 FEZ Domino Application1

I think I am having a conceptual issue on how the System.IO.Directory is related to the e.Volume.Rootdirectory.

regards,
Aaron

Your current directory needs to start at \sd\

[quote]
I think I am having a conceptual issue on how the System.IO.Directory is related to the e.Volume.Rootdirectory.[/quote]

Dunno about that, but the error the compiler is pointing out is actually a common C# error (particularly common if you’re used to programming in VB.net) - GetCurrentDirectory is a method, so you have to be sure to include the parens after it, like so:

System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\" + sFolder);

HTH

To enumerate Directories use DirectoryInfo.

Instantiate a DirectoryInfo object for the root directory, and then do a GetDirectories() method on the DirectoryInfo object. This will return an array of DirectoryInfo objects for the contained directories. I think you can take from here.

I have gotten it to work by doing the following:

dir = files_and_folders[Screenselection];  
string[] files = Directory.GetFiles(dir);
string[] folders = Directory.GetDirectories(dir);

It seems to work pretty well, I am not sure if this is the best method of doing it.
is there a website for references that the FEZ users go to check this sort of problems?

Thanks for the replies.

Good way to find a solution is to use Google. Putting in “c# enumerating directories” would have given you the answer.

VB requires the parenthesis too, I was just typing by hand and forgot them…no auto-complete on the forum :-[

GetDirectories does work just fine but I think he was after a GetDirectories without having to specify the full path everytime and possibly not having a Directory object handy.

Say there’s an event that fires when you “click” a directory and you haven’t got a public directory object handy…


private static void DirectoryClicked(string DirectoryName)
{
 // Yes I'm just ASSUMING there's no slash at the end for the demo code.
 string curDir = Directory.GetCurrentDirectory() + "\\"; 
 string[] dirs = Directory.GetDirectories(curDir);
 for (int i = 0; i < dirs.Length; i++)
 {
  Debug.Print("Directory: " + dirs[i]);
 }
 Directory.SetCurrentDirectory(curDir);
}

That will work every single time without tracking anything.

Skewworks:

[quote]VB requires the parenthesis too, I was just typing by hand and forgot them…no auto-complete on the forum :-[
[/quote]

No reason to use the “embarrassed” emoticon - I wasn’t “dis”-ing your typing skills, just pointing out that if one isn’t used to typing them thanks to VB’s autocomplete, then one may be more likely to forget to type them in C# - at least I am.

Skewworks:

[quote]GetDirectories does work just fine but I think he was after a GetDirectories without having to specify the full path everytime and possibly not having a Directory object handy.
[/quote]

As I pointed out in my original reply, I wasn’t trying to address Symbolick’s issue with GetDirectories - I was trying to answer the part of his problem where he was getting a compiler error - which was caused by the lack of parens.

Symbolick:

Yes, tinyclr.com :slight_smile:

One potential issue with simply googling your C# issues is that results returned may only apply to the full framework, and may not work in the micro framework. When I resort to google while working with my FEZ I always try to include the phrase “micro framework.”

Yes, especially with more complex work-arounds google can be a real pain, because most ersults will be handled with the “normal” framework.

Always include the “micro framework” or “dot net micro framework” words.

The GHIelectronic demo has a fantastic file browser class already
Can you not modify the class to suit your app

Ian

Being new to C# I am having trouble identifying which library I am using when I am making these directory calls. This whole framework thing is really sweat, but I am a slow learner. For instance:

Directory.GetFiles(dir);

Is this a call using the GHI extensions or using the microsoft.net extensions?

System.IO

I am also quite new to C# but as its the same layout as java and visual basic you can learn how to do, what you need to do by just opening the object browser looking at the various classes available…

For instance, as Mike said open the object browser and click on system.IO and just view all the classes and methods available each one gives a description of use then microsoft.netmf pages gives you a lot more…

If youre not sure of parameters for any method, the IDE will promt you anyway.