【发布时间】:2015-04-03 08:06:55
【问题描述】:
我正在尝试创建一个具有特定长度的新文件。 通过使用下面的代码,文件被创建。 问题是创建的文件长度为 0kb。 另外,stream writer 下的文本也没有写入文件中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace Filesize
{
class Program
{
static void Main(string[] args)
{
FileInfo fi = new FileInfo(@"D:\\demotext2.txt");
StreamWriter sw = new StreamWriter(@"D:\\demotext2.txt", true);
try
{
while (fi.Length >= (2 * 1024 * 1024))
{
sw.WriteLine("Demo File");
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
【问题讨论】:
-
在调试器中单步调试代码。从那里应该很明显。
-
应该是
fi.Length <= (2 * 1024 * 1024) -
@CarbineCoder 可能已经回答了您的问题。您还应该知道,仅在您的异常上调用
ToString不会给您任何信息。你想要Console.WriteLine(ex.ToString());吗?
标签: c#