-4
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.
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42

3 Answers3

4

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;
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
2

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.

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
1

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.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
prvnsaini
  • 186
  • 2
  • 12