6 Replies - 396 Views - Last Post: 02 December 2013 - 05:31 PM Rate Topic: -----

#1 Lucifer c#  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 13

I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 05:23 AM

string typos="*.txt";
string fesi=textBox1.Text ;

string[] arxeia = Directory.GetFiles(fesi, typos, SearchOption.AllDirectories);




it searchs for .txt files in the specified path i told it to search ( FOR EXAMPLE : C:\filesfolder\)
but if i use just : C:\ to search the entire drive .. it gives me "access denied" error when it goes to system protected folders ..
so can anyone tell me how to make it ignore those folders and search in others folders ?

Is This A Good Question/Topic? 0
  • +

Replies To: I have a question about System.IO.Directory.GetFiles() command ..

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3472
  • View blog
  • Posts: 10,706
  • Joined: 05-May 12

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 06:58 AM

C# question. Moving to C# forum...
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3472
  • View blog
  • Posts: 10,706
  • Joined: 05-May 12

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 07:04 AM

You can't. You'll need to be more granular about your search paths, or run as administrator. I recommend the former.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 10:45 AM

You'll have to roll your own. The key is using a try catch.

Explaining how to do this is pretty much the code, so... please read and understand what's going on.
// returns a list of paths found
List<string> FindFiles(string path, string searchPattern) {
	var results = new List<string>();
	// a queue to hold the directories left to be scanned
	var q = new List<DirectoryInfo>();
	// add the first directory
	q.Add(new DirectoryInfo(path));
	// while the queue has elements, process
	while (q.Count > 0) {
		var dir = q[0]; // grab the first dir
		q.RemoveAt(0); // remove it from the queue
		// important, prepare to fail
		try {
			// add any files found to results
			results.AddRange(dir.GetFiles(searchPattern).Select(fi => fi.FullName));
			// add directories to queue
			q.AddRange(dir.GetDirectories());
			// if we got this far, we had access
			Debug.WriteLine("SCANNED: " + dir.FullName);
		} catch (System.UnauthorizedAccessException ex) {
			// if we landed here, we can't process that directory
			Debug.WriteLine("DENIED: " + dir.FullName);
		}
	}
	// return what we found
	return results;
}


Was This Post Helpful? 2
  • +
  • -

#5 Lucifer c#  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 13

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 12:47 PM

Thank you guys! u helped me a lot .. i understand what i have to do !
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 02:06 PM

For shame, using a List<T> for a queue when we have Queue<T> :)

Change line 5 to var q = new Queue<DirectoryInfo>();
Change line 7 to q.Enqueue(new DirectoryInfo(path));
Change line 10 to var dir = q.Dequeue();;
Delete line 11.

You get the idea :)
Was This Post Helpful? 1
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: I have a question about System.IO.Directory.GetFiles() command ..

Posted 02 December 2013 - 05:31 PM

Really?

Queue forces me to add one element at a time. Where List.AddRange does it all in one. That's one of a couple of reasons it lost to list. ( I actually had considered it. )

True, I need two operations to pop the head, but so what? Should I assume Queue is optimized so thoroughly, that it's such a significantly better choice, that I should slavishly use it on those rare occasions it's actually appropriate?

More importantly, would its use have made a better example? Rather, it seemed that offering two instances of List, both taking advantage AddRange, was a cleaner option.

You are welcome to disagree. But, "for shame?!" Seriously? :P
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1