Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Greetings,

 

I know this is prob. is easy - but I am just starting out with C# and looking to do some experiments to learn faster.

 

I would like a listbox to show all xml files in the current directory and all subdirectories. The xml file list should be sorted. When a user clicks on one of these files, it will open it in notepad.

 

I'm sure this is easy to do - can you share how you would accomplish it?

 

Thanks

 

-mreider

  • Leaders
Posted

Well, showing all the XML files in a directory is easy to do. Directory.GetFiles() will do most of the work for you.

 

Finding the XML files in subdirectories is harder, because you first have to find the (sub)directories in the directory and then do the above to find all of the files in this directory. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted
Finding the subdirectories should actually be pretty trivial if you are familiar with recursion (in terms of programming). As far as opening them in notepad, look into the System.Diagnostics.Process class.
[sIGPIC]e[/sIGPIC]
Posted
Greetings,

 

I know this is prob. is easy - but I am just starting out with C# and looking to do some experiments to learn faster.

 

 

Just in case you need extra information, here is a sample you can tweak.

 

private void OnFormLoad(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();

if (fbd.ShowDialog() == DialogResult.OK)
{
	GetAllFiles(fbd.SelectedPath);
}

lbFiles.Sorted = true;
}

private void GetAllFiles(string startPath)
{
string[] files = Directory.GetFiles(startPath, "*.xml");
foreach (string s in files)
	lbFiles.Items.Add(s);

string[] dirs = Directory.GetDirectories(startPath);
foreach (string dir in dirs)
	GetAllFiles(dir);
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...