473,469 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Random Directories

Hi too all,

I'm trying to write a simple program which will find all the folders in the
c drive, but i'm having problems
I've managed to search the root of c drive folders but having problems with
search them. Here is the code i've been so far

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{
static void Main(string[] args)

{

string[] folders = Directory.GetDirectories(@"C:\");

int p = folders.Length;

System.Random RandNum = new System.Random();

int mRandomNumber = RandNum.Next(1, p);

Console.WriteLine(folders.GetValue(mRandomNumber)+ @"\");

}

}

}

can someone offer a helping hand?

thanks

Nov 17 '05 #1
2 1407
Hi Default User,

You aren't saying what specific problem you have, but I suspect you mean you can't get the subdirectories.

You need to recursively call a method that retrieves directory names until there are none left.

static void Main()
{
ArrayList list = new ArrayList();
GetDirectories("C:\", list);
Random rand = new Random();
int num = rand.Next(0, list.Count);
Console.WriteLine(list[num]);

// Don't use the line below. I merely added it to show that
// you can in fact combine the above three lines.
// For clarity reasons and readability you should not use this.
// Console.WriteLine(list[new Random().Next(0, list.Count)]);
}

static void GetDirectories(string dir, ArrayList storage)
{
string[] dirs = Directory.GetDirectories(dir);
foreach(string s in dirs)
{
storage.Add(s);
GetDirectories(s, storage);
}
}

Of course, you will now bump into exceptions when you can't read the subdirectory. To solve this you can use a try/catch block and ignore those directories that throw an exception

try
{
GetDirectories(s, storage);
}
catch
{
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
On Sat, 17 Sep 2005 06:01:27 +0100, "Default User"
<an*******@discussions.microsoft.com> wrote:
Hi too all,

I'm trying to write a simple program which will find all the folders in the
c drive, but i'm having problems
I've managed to search the root of c drive folders but having problems with
search them. Here is the code i've been so far

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{
static void Main(string[] args)

{

string[] folders = Directory.GetDirectories(@"C:\");

int p = folders.Length;

System.Random RandNum = new System.Random();

int mRandomNumber = RandNum.Next(1, p);

Console.WriteLine(folders.GetValue(mRandomNumber) +@"\");

}

}

}

can someone offer a helping hand?

thanks

Have a look at the foreach loop, and run it through your folders
array.

rossum

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
4
by: SoulSniper | last post by:
Hi, I have been stuck on this for a few days now and have given up trawling through pages and pages of google results.. I'm just putting the finishing touches to a small game I've written....
1
by: Heath | last post by:
I'm dealing with a C# application that monitors changes to the file system, and need to exclude irrelevent directories, temp directories for example. Is there any way to identify such...
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
6
by: =?Utf-8?B?WW9naSBXYXRjaGVy?= | last post by:
Hello, I am using Visual Studio-2003. I created a project to build my library. Since I am using third party libraries as well, I have specified those additional library dependencies in project...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
6
by: Lanny | last post by:
Well the othe day I was making a program to make a list of all the songs in certian directorys but I got a problem, only one of the directorys was added to the list. Heres my code: import random...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.