开发者

Verify the size of an file and if it is bigger then X empty it

开发者 https://www.devze.com 2023-02-20 16:29 出处:网络
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 siz

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, "");
}       
0

精彩评论

暂无评论...
验证码 换一张
取 消