RosettaCodeData/Task/Truncate-a-file/C-sharp/truncate-a-file.cs
2023-07-01 13:44:08 -04:00

28 lines
835 B
C#

using System;
using System.IO;
namespace TruncateFile
{
internal class Program
{
private static void Main(string[] args)
{
TruncateFile(args[0], long.Parse(args[1]));
}
private static void TruncateFile(string path, long length)
{
if (!File.Exists(path))
throw new ArgumentException("No file found at specified path.", "path");
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
if (fileStream.Length < length)
throw new ArgumentOutOfRangeException("length",
"The specified length is greater than that of the file.");
fileStream.SetLength(length);
}
}
}
}