-6

How can i assign Property System.IO.FileInfo.Length?

FileInfo.Length Property

It is read only!

SilverLight
  • 19,668
  • 65
  • 192
  • 300

2 Answers2

5

FileInfo.Length is used to get the size of a file, the only way to change a file size is by modifying it's content

Eli Perl
  • 146
  • 1
  • 9
  • 9
    @MoonLight : But he's right. Now you're just being ungrateful. It's read-only. You cannot change it. Period. And I would still like to know _why_ you need to do this. – Visual Vincent Apr 04 '16 at 22:26
  • 1
    @MoonLight - I don't understand - what would be the purpose of removing the answer to "let other people answer"? It doesn't make any sense to do, especially if it's correct – Krease Apr 05 '16 at 01:24
1

If you want to set a file's size programmatically you would have to write something to it. The simplest way would probably be to just fill it with zeros.

public void writeEmptyFile(string path, int size)
{
    using(FileStream fs = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.ReadWrite))
    {
        fs.Write(new byte[size], 0, size);
    }
}

This method will either A) write a new file containing null-bytes (the file will have the specified size), or B) if the file path already exists it will append the specified amount of null-bytes to that file, increasing it's size.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75