Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Truncate-a-file/C-sharp/truncate-a-file.cs
Normal file
28
Task/Truncate-a-file/C-sharp/truncate-a-file.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue