System.IO.FileStream.SetLength C# (CSharp) Method

SetLength() public method

Sets the length of this stream to the given value.
public SetLength ( long value ) : void
value long The new length of the stream.
return void
        public override void SetLength(long value)
        {
            if (value < 0)
                throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum);
            if (_fileHandle.IsClosed)
                throw Error.GetFileNotOpen();
            if (!CanSeek)
                throw Error.GetSeekNotSupported();
            if (!CanWrite)
                throw Error.GetWriteNotSupported();

            SetLengthInternal(value);
        }

Usage Example

Beispiel #1
0
        private void GenerateBackups()
        {
            var rand = new Random();
            foreach (var path in paths)
            {
                int gb = rand.Next(1, 3);
                var writepath = string.Format("{0}\\{1}.tib", path, DateTime.Now.ToString("MMM-dd-yyyy-fffffff"));

                //50% failure rate per folder!
                if (rand.NextDouble() >= 0.5)
                {
                    //write a file to disk with a filesize between 1-3gb
                    using (var fs = new FileStream(writepath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        try
                        {
                            fs.SetLength(1024 * 1024 * 1024 * gb);
                        }
                        catch (Exception)
                        {
                            fs.SetLength(1024 * 1024 * 1024);
                        }

                    }
                    Console.WriteLine("\tWriting {0} ({1}Gb)", writepath, gb);
                }
                else Console.WriteLine("\tSimulating failure for {0}", path);

            }
        }
All Usage Examples Of System.IO.FileStream::SetLength