WriteAllText似乎没有被调用
本文关键字:调用 WriteAllText | 更新日期: 2023-09-27 18:30:28
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.
这不会解决你的问题,但至少会缩短你的代码并使其可维护。将您的代码替换为以下内容,它将执行相同的操作。
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;
在 Form1_Load 中的第一个语句上放置一个断点,看看它是否被命中。如果没有,则可能需要在代码中订阅此事件。
如果它被命中,请单步执行代码并找到它失败的行。
请注意,默认情况下,Form_Load 不会捕获异常,因此看起来好像跳过了其他行。有办法解决它,只需按照上面的链接。
我认为您提供的目录和路径不存在或错误,因此在尝试获取信息时会引发异常
var directoryInfo11 = new System.IO.DirectoryInfo(@"q:'FTP'");
在你的代码周围添加 try catch 块,看看它是否抛出。