我寫的文字使用下面的代碼使用StreamWriter
文件:創建文本文件,如果動態文本文件大小超過最大容量
path == @"Desktop\";
filepath1 = path + "1.txt";
StreamWriter _sw = new StreamWriter(filepath1, true);
_sw.WriteLine("some Text");
_sw.Close();
如果文本文件的大小超過500KB我要創建的文本文件動態。我tryng下面的代碼:
var size = (path.Length)/1024;
if(size>=500)
{
int i = (size/500)+1;
var filepath2 = path + i + ".txt";
if (File.Exists(filepath2))
{
StreamWriter _sw = new StreamWriter(filepath2, true);
_sw.WriteLine("Some message");
_sw.Close();
}
}
else
{
FileStream fs = File.Create(filepath2);
StreamWriter _sw = new StreamWriter(filepath2, true);
_sw.WriteLine(ex);
_sw.Close();
}
我的問題是,如果文件2.txt也超過500KB我想創建3.txt,4.txt .....等等.. 我想動態創建所有這些 - 如何解決這個問題?