Verify the size of an file and if it is bigger then X empty it
I would like to verify the size of a folder and if it is bigger th开发者_JAVA技巧en 2 gb i should empty it . How can i do so ?
You can use the Length property of the FileInfo class to get the file size, then truncate the file with WriteAllText() or similar:
using System.IO;
if (new FileInfo(yourFilePath).Length > Int32.MaxValue) {
File.WriteAllText(yourFilePath, String.Empty);
}
FileInfo f = new FileInfo("path to file");
if (f.Length > 2147483648)
File.Delete("path to file");
FileInfo fInfo = new FileInfo(@"c:\temp\test.txt");
if (fInfo.Length> 2147483648)
{
File.WriteAllText(fInfo.FullName, "");
}
精彩评论