Today we received a task –create a TreeView Control in C# and list all files and directories within. Fair enough. You start off with grabbing the TreeView control onto your form, then you start writing your code, recursive directory calls…etc. You put it together. You start it off and the first thing that hits you in the face is the amount of time that you have to wait for your application to populate the TreeView.
My first thing was to modify the exercise; I added a ComboBox Control, populated it with the available drives and then created the SelectedIndexChanged() event which launched the population of the TreeView.
Life is easy, this exercise is easy - one might say. If you run this program on your local machine (or even better, on a laptop) you have no problem at all.
The first problem you might come across is the fact that older machines still have the precious “A:\” drive. Why is that a problem? When you populate your ComboBox it will check whether the drive is available. “A:\” drive is active and available, therefore it wants to go deeper into it and well, the drive is empty. Two possibilities exist to eliminate this error.
1. Try-catch
But we definetly don’t want to go for the usual solution.
2. Instead of creating a string array to display the drives:
string[] drives = Environment.GetLogicalDrives();
you could create a DriveInfo array as shown below:
DriveInfo[] drives = DriveInfo.GetDrives();
(note that if you don’t use the System.IO namespace you need to write System.IO.DriveInfo.GetDrives();).
You then use a foreach loop to populate your ComboBox and you use the .IsReady() method to determine whether a drive is ready.
foreach (DriveInfo aDrive in drives)
{
if (aDrive.IsReady == true)
{
comboBox1.Items.Add(aDrive);
}
}
Okay, now you got rid of the problem, your code runs smoothly. Again, life is not that easy
When I tried to run my code at the uni, it keept throwing me a SecuritException. As you can imagine there are various policies used on the machines back there, with different permissions, etc. After doing some research I managed to find a great article on how to avoid this error message.
Let me introduce the procedure in brief. What you have to do first, is to create a Strong Name Key Pair. You can achieve this by running Visual Studio 2005 Command Prompt. There simply enter “sn -k mykey.snk“. This will create the appropriate file (mykey.snk) for you. Don’t forget to copy this to your actual project folder!!! If the file is at the correct location open the AssemblyInfo.cs file within Visual Studio. (This file can be found in the Solution Explorer, under the “Properties”). Add the following line to your code:
[assembly: AssemblyKeyFile("mykey.snk")]
(note, you don’t put a semicolon after this line!)
Now you need to modify your security policy with the Microsoft .NET Framework Configuration utility. To start this up, enter “mscorcfg.msc” to your Visual Studio 2005 Command Prompt. Navigate to the following: .NET Framework X(version) Configuration > Runtime Security Policy > Machine > Code Groups > All_Code. (as shown on picture)

Here do the following:
- Right click on All_Code, and select New
- Create a new code group for your strong name, and hit next
- Select a strong name membership condition from the drop down box
- Hit the import button, and select your assembly. The configuration tool will import your public key. If you want to trust everything you sign with this key, leave the name and version boxes unchecked
- Select the FullTrust permission set
(at step 4 please note that an assembly is a .dll or a .exe file).
After you are done with the previous steps continue with these:
- Expand the All_Code code group
- Right click the LocalIntranet_Zone code group, and select properties
- Switch to the Permission Set tab, and select FullTrust
If you now compile and run your program, the SecurityException won’t appear! For further information and detailed explanation I recommend that you read the article previously mentioned.