Click here to Skip to main content
15,902,939 members

Comments by kavithakumari (Top 1 by date)

kavithakumari 25-Jan-13 2:18am View    
hi,
actually i want read all excel file name with extensions from the system and that names saved in to the folder .

i am writing some code i.e
private void button1_Click(object sender, EventArgs e)
{

foreach (string file in GetFiles("C:\\"))
{
// Console.WriteLine(file);
listBox2.Items.Add(file);
}

}


static IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (Exception ex)
{
MessageBox.Show("ex");
Console.Error.WriteLine(ex);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];

}
}
}
}

but this code shows one error .error is

Access to the path 'C:\Documents and Settings' is denied.

how to rectify this error.