Category Archives: Tutorials

Tutorials.

[C#] How to get all folders and sub-folders within a path?

2
Filed under C\C++\C#, General, How to?, Programming, Source code, Tutorials
Tagged as , , , ,

Hi,

As you probably may know, C# library has a function that allows you to get all folders and sub-folders within a certain path. Even though it’s simple and easy to use, you can face a few problems when trying to access files that for some reason are not accessible. This simply throws an exception and the application fails.
When searching for sub-folders, there is no way to get all the exceptions thrown since they can be quite a lot, so the solution is to do a ‘get folders and sub-folders’ function yourself. Note that when we simply want the folders within a path (no sub-folders) then it is possible to catch the exceptions.
The code provided below was used in my project TextFindIT.

The function below will get all folders and sub-folders within a path and will add the results to a List of strings.

1
2
3
   //Get folders in a path
   List<string> folders = new List<string>(); // Our list of folders where we will keep the results
   folders = GetAllFolders(path); // Call the function

So the code above will just call our function and send a valid path path.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
   //Returns all folders inside a folder, including sub-folders
   private List<string> GetAllFolders(string path)
   {
       List<string> folders = new List<string>(); // The list of results to be returned
       folders.Add(path); // Our first result is our path (top folder)
 
       // For each folder in our path, we will add the folder to the list, and check for sub-folders within that folder.
       foreach (string folder in GetFolders(path))
       {
          folders.Add(folder); // Add the folder as a result
          folders.AddRange(GetAllFolders(folder).ToList()); // Get all folders and sub-folders within the folder
       }
 
       return folders; // Return the list of results
   }
 
   //Returns all folders inside a folder (top only)
   private string[] GetFolders(string path)
   {
      string[] folders = { }; // Array of results
 
      // Below we try to add the folders inside 'path' and catch any possible exceptions.
      try
      {
         folders = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
      }
      catch
      {
         //catch exceptions to avoid breaking the search
      }
 
      return folders; // Return the results
   }

So basically, for each folder inside our path, we will check for sub-folders, and for each sub-folder we will also check for sub-folders, and so on…
Notice the line folders.AddRange(GetAllFolders(folder).ToList());. We are calling the function GetAllFolders while still inside of that same function. This is called Recursion. Also, because GetAllFolders returns a List of strings with the results, we must do .AddRange(...) to add to our main List of strings the entire list of results (i.e. add a List to a List).
In the second function, we just put in an array all the folders inside path by doing Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly); while catching any possible exception. path is our current path, * is the search pattern (in this case it’s anything), and the last parameter SearchOption.TopDirectoryOnly is our search options (top only). Because we are not trying to go for sub-folders it is possible to do this (catch the exceptions), otherwise it wouldn’t.

And I guess that’s basically it.
Hope you find it useful.

TextFindIT Video Demonstration

1
Filed under General, How to?, News, Projects, Tutorials
Tagged as , , , , ,

I’ve just uploaded a short video demonstration of TextFindIT.
You can probably view it better in youtube with HD enabled. Enjoy.

Creating an exception in Avast.

1
Filed under General, How to?, Problems/Solutions, Tutorials
Tagged as , , , , , , , , ,
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...

Hello there, long time no see…been busy with college.

Anyway…this post is about creating exceptions in avast.
If you have a file/folder/URL that avast detects as being/containing a virus, although you know it’s not, you can add the file/folder/URL as an exception to Avast. This can definitely be very useful.
I made this using Avast Home Edition 4.8.
But first…

Please note: crwld.org is not responsible for any damage you may do to your computer by following the instructions bellow.

So…you can add exceptions to files/folders and to Internet web sites.

To add a web site as an exception to Avast, left-click the Avast icon on the tray bar. A window will open.
On your left side you should be able to see Avast installed providers…if not, just click where it says ‘Details… >>‘.

When you can finally see the providers, select the ‘Web Shield‘ provider and click ‘Customize‘, ‘Exceptions‘, and in the ‘1. URLs to exclude:‘ box, press ‘Add…‘. After that, just write the URL you want to add as an exception. If you want all the ’sub-folders’ under that address to be also considered an exception you should add an ‘*‘ to the end of the URL…like this:

(example)

http://thiswebsitedoesnotcontainanyvirus.com/*

That should do it.
You can also exclude extensions as indicated in the label below the box, by writing the extension name:

(example)
*.txt

When you’re done adding exceptions just click ‘OK‘.

If you want to add a file/folder on your hard disk as an exception, you want to select the ‘Standard Shield‘ provider, and then click the ‘Advanced‘ tab. As in the ‘Web Shield‘ provider, you should see a small box where you can read ‘Here you can modify the list of locations that will not be scanned and/or tested (global exclusions are not appended):‘, which means ‘Here you can add an exception’.
So, you probably figured it out already…you have to go and click the ‘Add‘ button and write the path to the file/folder you want to add as an exception. Once again, if you wish to include sub-folders (if adding a folder), you should add an ‘*‘ to the end of the path…

(example)
C:\Users\MeAndMyself\TheseAreNotVirus\*

or

C:\Users\MeAndMyself\TheseAreNotVirus\notavirus.exe

Click ‘OK‘ and again ‘OK‘ when you’re done to exit.

And this is how you can add exceptions to Avast…don’t forget that the path’s that you add will not be scanned or will be ignored by Avast, so…remember that.
Hope this helped someone.

Take care.
Merry Christmas!

Google Code Jam 08 – Alien Numbers

0
Filed under C\C++\C#, General, Problems/Solutions, Programming, Source code, Tutorials
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...

Hi there

Here is my solution for the first practice exercise of Google Code Jam 2008, called Alien Numbers.
I haven’t had much time to code recently, due to exams, but didn’t resist and took a look at the first exercise :P
I did it in C.

Read More »