-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.
4

3 回答 3

4

这不会解决您的问题,但至少会缩短您的代码并使其可维护。用以下代码替换您的代码,它会做同样的事情。

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));

不要忘记在文件顶部添加以下内容。

using System.IO;
于 2013-09-07T18:20:16.730 回答
2

在 Form1_Load 的第一条语句上放置一个断点,看看它是否被命中。如果没有,您可能需要在代码中订阅此事件。

如果它被击中,请单步执行您的代码并找到它失败的行。

请注意,Form_Load 默认情况下不会捕获异常,因此看起来好像跳过了其他行。有办法解决它,只需按照上面的链接。

于 2013-09-07T18:32:11.807 回答
1

我认为您提供的此目录和路径不存在或错误,因此在尝试获取信息时抛出异常

var directoryInfo11 = new System.IO.DirectoryInfo(@"q:\FTP\");

在您的代码周围添加 try catch 块,看看它是否抛出异常。

于 2013-09-07T18:38:17.310 回答