I am trying to save GetDirectories as a txt file, but somewhere my program fails.
private void Form1_Load(object sender, EventArgs e)
{
var directoryInfo = new System.IO.DirectoryInfo(@"g:\FTP\");
int directoryCount = directoryInfo.GetDirectories().Length;
...
var directoryInfo11 = new System.IO.DirectoryInfo(@"q:\FTP\");
int directoryCount11 = directoryInfo11.GetDirectories().Length;
int directoryCountMain = directoryCount + directoryCount2 +
directoryCount3 + directoryCount4 + directoryCount5 +
directoryCount6 + directoryCount7 + directoryCount8 +
directoryCount9 + directoryCount10 + directoryCount11;
string text = "Total Releases: ";
// WriteAllText creates a file, writes the specified string to the
// file, and then closes the file.
System.IO.File.WriteAllText(@"c:\test\ik.txt", text + directoryCountMain);
}
I don't get an error or anything, It looks like my code is skipped as I tried placing a MessageBox.Show below the code but It got ignored.

- 3,249
- 3
- 24
- 42

- 9
- 1
- 4
-
Where did you place the message box?. Explain your problem a little more. – Nithin Nayagam Sep 07 '13 at 18:17
-
At the end, all I want is to get all folders from the hard drives and save the amount in a txt file, but I dont get nothing. – Ingela Törner Sep 07 '13 at 18:20
-
It looks skipped probably because it *is skipped* - verify that `Load event` is actually subscribed to. – Nikola Markovinović Sep 07 '13 at 18:25
-
2Debug it. Step through it. – usr Sep 07 '13 at 18:29
3 Answers
This won't solve your problem, but at least will shorten your code and make it maintainable. Replace your code with following, it will do the same.
var ftpDirs = new string[] { "g:/FTP/", ... };
int subDirsCount = 0;
foreach(var dir in ftpDirs)
{
subDirsCount += new DirectoryInfo(dir).GetDirectories().Length;
}
string text = "Total Releases: ";
File.WriteAllText(@"c:\test\ik.txt", string.Format("{0}{1}", text, subDirsCount));
Do not forget to add following at the top of the file.
using System.IO;

- 12,486
- 14
- 59
- 93
-
-
6@DavidHeffernan My answer is not the type of answer I would provide in comments. It is long enough that it deservs a place among other answers. And I stated there, that it is not a direct answer, but just a helpful idea. – Ondrej Janacek Sep 07 '13 at 18:43
Place a breakpoint on the first statement in Form1_Load and see if it gets hit. If not, you probably need to subscribe to this event in your code.
If it gets hit, step through your code and find the line where it fails.
Note that Form_Load does not catch exceptions by default, so it will appear as though other lines were skipped. There are ways to solve it, just follow the above link.

- 1
- 1

- 25,801
- 18
- 85
- 151
I think this directories and path you have given doesnt exists or wrong therefore throwing exception when trying to get info
var directoryInfo11 = new System.IO.DirectoryInfo(@"q:\FTP\");
Add try catch block around your code and see if its throwing excpetion.

- 12,486
- 14
- 59
- 93

- 186
- 2
- 12